

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=dark>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/memory_blog/img/fluid.png">
  <link rel="icon" href="/memory_blog/img/icon/dw.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="Memory">
  <meta name="keywords" content="">
  
    <meta name="description" content="🍖 记录个人对算法学习过程的体验与总结，分享解决问题时的有效思路和方法">
<meta property="og:type" content="article">
<meta property="og:title" content="探索算法学习之旅：经验分享与解题思路">
<meta property="og:url" content="https://test.atomgit.net/memory_blog/2023/07/17/%E6%8E%A2%E7%B4%A2%E7%AE%97%E6%B3%95%E5%AD%A6%E4%B9%A0%E4%B9%8B%E6%97%85%EF%BC%9A%E7%BB%8F%E9%AA%8C%E5%88%86%E4%BA%AB%E4%B8%8E%E8%A7%A3%E9%A2%98%E6%80%9D%E8%B7%AF/index.html">
<meta property="og:site_name" content="Memory&#39;s blog">
<meta property="og:description" content="🍖 记录个人对算法学习过程的体验与总结，分享解决问题时的有效思路和方法">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://test.atomgit.net/memory_blog/img/artical/sky9.jpg">
<meta property="article:published_time" content="2023-07-17T07:51:44.000Z">
<meta property="article:modified_time" content="2024-04-24T08:16:47.000Z">
<meta property="article:author" content="Memory">
<meta property="article:tag" content="经验">
<meta property="article:tag" content="算法">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://test.atomgit.net/memory_blog/img/artical/sky9.jpg">
  
  
  
  <title>探索算法学习之旅：经验分享与解题思路 - Memory&#39;s blog</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/memory_blog/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/memory_blog/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/memory_blog/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"test.atomgit.net","root":"/memory_blog/","version":"1.9.4","typing":{"enable":true,"typeSpeed":100,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":false,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":true,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/memory_blog/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/memory_blog/js/utils.js" ></script>
  <script  src="/memory_blog/js/color-schema.js" ></script>
  

  

  

  

  

  

  

  



  
<meta name="generator" content="Hexo 5.4.2"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 75vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/memory_blog/">
      <strong>Memory&#39;s blog</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/memory_blog/">
                <i class="iconfont icon-home-fill"></i>
                <span>首页</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/memory_blog/archives/">
                <i class="iconfont icon-archive-fill"></i>
                <span>归档</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/memory_blog/categories/">
                <i class="iconfont icon-category-fill"></i>
                <span>分类</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/memory_blog/tags/">
                <i class="iconfont icon-tags-fill"></i>
                <span>标签</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/memory_blog/about/">
                <i class="iconfont icon-user-fill"></i>
                <span>关于</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/memory_blog/links/">
                <i class="iconfont icon-link-fill"></i>
                <span>友链</span>
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              <i class="iconfont icon-search"></i>
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">
              <i class="iconfont icon-dark" id="color-toggle-icon"></i>
            </a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/memory_blog/img/newBG/girl.jpg') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="探索算法学习之旅：经验分享与解题思路"></span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        Memory
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2023-07-17 15:51" pubdate>
          2023年7月17日 下午
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          74k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          185 分钟
        
      </span>
    

    
    
      
        <span id="leancloud-page-views-container" class="post-meta" style="display: none">
          <i class="iconfont icon-eye" aria-hidden="true"></i>
          <span id="leancloud-page-views"></span> 次
        </span>
        
      
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar category-bar" style="margin-right: -1rem">
    





<div class="category-list">
  
  
    
    
    
    <div class="category row nomargin-x">
      <a class="category-item 
          list-group-item category-item-action col-10 col-md-11 col-xm-11" title="算法"
        id="heading-046a899ee7a6ec88d370211a518c9e80" role="tab" data-toggle="collapse" href="#collapse-046a899ee7a6ec88d370211a518c9e80"
        aria-expanded="true"
      >
        算法
        <span class="list-group-count">(1)</span>
        <i class="iconfont icon-arrowright"></i>
      </a>
      
      <div class="category-collapse collapse show" id="collapse-046a899ee7a6ec88d370211a518c9e80"
           role="tabpanel" aria-labelledby="heading-046a899ee7a6ec88d370211a518c9e80">
        
        
          
          
  <div class="category-post-list">
    
    
  </div>

          
  
    
    
    
    <div class="category-sub row nomargin-x">
      <a class="category-subitem 
          list-group-item category-item-action col-10 col-md-11 col-xm-11" title="经验"
        id="heading-32a8e000e928e6dc255876d9fc0178b3" role="tab" data-toggle="collapse" href="#collapse-32a8e000e928e6dc255876d9fc0178b3"
        aria-expanded="true"
      >
        经验
        <span class="list-group-count">(1)</span>
        <i class="iconfont icon-arrowright"></i>
      </a>
      
      <div class="category-collapse collapse show" id="collapse-32a8e000e928e6dc255876d9fc0178b3"
           role="tabpanel" aria-labelledby="heading-32a8e000e928e6dc255876d9fc0178b3">
        
        
          
  <div class="category-post-list">
    
    
      
      
        <a href="/memory_blog/2023/07/17/%E6%8E%A2%E7%B4%A2%E7%AE%97%E6%B3%95%E5%AD%A6%E4%B9%A0%E4%B9%8B%E6%97%85%EF%BC%9A%E7%BB%8F%E9%AA%8C%E5%88%86%E4%BA%AB%E4%B8%8E%E8%A7%A3%E9%A2%98%E6%80%9D%E8%B7%AF/" title="探索算法学习之旅：经验分享与解题思路"
           class="list-group-item list-group-item-action
           active">
          <span class="category-post">探索算法学习之旅：经验分享与解题思路</span>
        </a>
      
    
  </div>

        
      </div>
    </div>
  
        
      </div>
    </div>
  
</div>


  </aside>


    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">探索算法学习之旅：经验分享与解题思路</h1>
            
              <p class="note note-success">
                
                  
                    本文最后更新于：2 个月前
                  
                
              </p>
            
            
              <div class="markdown-body">
                
                <p><img src="https://gitee.com/deng-2022/pictures/raw/master/images/sky9.jpg"></p>
<h1 id="破冰"><a href="#破冰" class="headerlink" title="破冰"></a>破冰</h1><blockquote>
<p><a target="_blank" rel="noopener" href="https://juejin.cn/post/6844903519632228365#comment">漫画：什么是红黑树？ - 掘金 (juejin.cn)</a></p>
</blockquote>
<h1 id="脑力激荡"><a href="#脑力激荡" class="headerlink" title="脑力激荡"></a>脑力激荡</h1><h2 id="链表基础"><a href="#链表基础" class="headerlink" title="链表基础"></a>链表基础</h2><h3 id="基础知识（青铜挑战）"><a href="#基础知识（青铜挑战）" class="headerlink" title="基础知识（青铜挑战）"></a>基础知识（青铜挑战）</h3><h4 id="单链表基础及构造方法"><a href="#单链表基础及构造方法" class="headerlink" title="单链表基础及构造方法"></a>单链表基础及构造方法</h4><h5 id="链表的内部结构"><a href="#链表的内部结构" class="headerlink" title="链表的内部结构"></a>链表的内部结构</h5><ul>
<li><h5 id="以下是我对单链表的理解：（2023-07-17午）"><a href="#以下是我对单链表的理解：（2023-07-17午）" class="headerlink" title="以下是我对单链表的理解：（2023/07/17午）"></a>以下是我对单链表的理解：（2023/07/17午）</h5></li>
</ul>
<blockquote>
<p>链表，是用来存储数据的一种数据结构，其由若干个节点依次连接而成。<br>一个节点就是一个数据元素，一个节点由两部分构成：数据域和指针域。<br>数据域存放数据元素的值，指针域存放指针，而该指针用来指向下一个节点。</p>
</blockquote>
<h5 id="链表的构造"><a href="#链表的构造" class="headerlink" title="链表的构造"></a>链表的构造</h5><ul>
<li><h5 id="链表的构造过程很简单："><a href="#链表的构造过程很简单：" class="headerlink" title="链表的构造过程很简单："></a>链表的构造过程很简单：</h5><ol>
<li><h5 id="创建头节点，创建head指针指向头节点"><a href="#创建头节点，创建head指针指向头节点" class="headerlink" title="创建头节点，创建head指针指向头节点"></a>创建头节点，创建head指针指向头节点</h5></li>
<li><h5 id="依次创建每个节点，初始化其数据域，并令前驱节点的指针域指向该节点"><a href="#依次创建每个节点，初始化其数据域，并令前驱节点的指针域指向该节点" class="headerlink" title="依次创建每个节点，初始化其数据域，并令前驱节点的指针域指向该节点"></a>依次创建每个节点，初始化其数据域，并令前驱节点的指针域指向该节点</h5></li>
<li><h5 id="链表创建完成，返回该链表的head指针"><a href="#链表创建完成，返回该链表的head指针" class="headerlink" title="链表创建完成，返回该链表的head指针"></a>链表创建完成，返回该链表的head指针</h5></li>
</ol>
</li>
<li><h5 id="下面给出具体的代码实现"><a href="#下面给出具体的代码实现" class="headerlink" title="下面给出具体的代码实现"></a>下面给出具体的代码实现</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">// 单链表节点</span><br>  <span class="hljs-keyword">static</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Node</span> &#123;<br>      <span class="hljs-comment">// 数据域</span><br>      <span class="hljs-keyword">public</span> <span class="hljs-type">int</span> val;<br>      <span class="hljs-comment">// 指针域</span><br>      <span class="hljs-keyword">public</span> Node next;<br>      <span class="hljs-comment">// 节点初始化</span><br>      Node(<span class="hljs-type">int</span> x) &#123;<br>          val = x;<br>          next = <span class="hljs-literal">null</span>;<br>      &#125;<br><br>      <span class="hljs-meta">@Override</span><br>      <span class="hljs-keyword">public</span> String <span class="hljs-title function_">toString</span><span class="hljs-params">()</span> &#123;<br>          <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;Node&#123;&quot;</span> +<br>                  <span class="hljs-string">&quot;val=&quot;</span> + val +<br>                  <span class="hljs-string">&quot;, next=&quot;</span> + next +<br>                  <span class="hljs-string">&#x27;&#125;&#x27;</span>;<br>      &#125;<br>  &#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">// 使用数组构建单链表</span><br>   <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> Node <span class="hljs-title function_">initLinkedList</span><span class="hljs-params">(<span class="hljs-type">int</span>[] array)</span> &#123;<br>       <span class="hljs-comment">// 1.定义head指针, cur指针</span><br>       <span class="hljs-type">Node</span> <span class="hljs-variable">head</span> <span class="hljs-operator">=</span> <span class="hljs-literal">null</span>, cur = <span class="hljs-literal">null</span>;<br>       <span class="hljs-comment">// 2.遍历数组, 构建单链表</span><br>       <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i &lt; array.length; i++) &#123;<br>           <span class="hljs-comment">// 2.1.新建节点, 依次获取数组元素, 并赋值给该节点的数据域</span><br>           <span class="hljs-type">Node</span> <span class="hljs-variable">newNode</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Node</span>(array[i]);<br>           <span class="hljs-comment">// 2.2.链表为空, 插入头节点</span><br>           <span class="hljs-keyword">if</span> (i == <span class="hljs-number">0</span>) &#123;<br>               <span class="hljs-comment">// 2.2.1.初始化head指针</span><br>               head = newNode;<br>               <span class="hljs-comment">// 2.2.2.更新cur指针, 指向新节点</span><br>               cur = newNode;<br>               <span class="hljs-comment">// 2.3.链表不为空, 插入后继节点</span><br>           &#125; <span class="hljs-keyword">else</span> &#123;<br>               <span class="hljs-comment">// 2.3.1.更新每个结点的指针域, 指向后继节点</span><br>               cur.next = newNode;<br>               <span class="hljs-comment">// 2.3.2.更新cur指针, 指向新节点</span><br>               cur = newNode;<br>           &#125;<br>       &#125;<br>       <span class="hljs-comment">// 3.单链表构建完成, 返回头指针</span><br>       <span class="hljs-keyword">return</span> head;<br>   &#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">// 测试</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> &#123;<br>        <span class="hljs-type">int</span>[] a = &#123;<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>&#125;;<br>        <span class="hljs-type">Node</span> <span class="hljs-variable">head</span> <span class="hljs-operator">=</span> initLinkedList(a);<br>        System.out.println(head);<br>    &#125;<br></code></pre></td></tr></table></figure>

<h5 id="遍历链表"><a href="#遍历链表" class="headerlink" title="遍历链表"></a>遍历链表</h5><ul>
<li><h5 id="打印链表：头指针依次向后移动，打印每个节点的数据域"><a href="#打印链表：头指针依次向后移动，打印每个节点的数据域" class="headerlink" title="打印链表：头指针依次向后移动，打印每个节点的数据域"></a>打印链表：头指针依次向后移动，打印每个节点的数据域</h5></li>
<li><h5 id="获取链表长度：头指针依次向后移动，累加节点个数，打印链表长度"><a href="#获取链表长度：头指针依次向后移动，累加节点个数，打印链表长度" class="headerlink" title="获取链表长度：头指针依次向后移动，累加节点个数，打印链表长度"></a>获取链表长度：头指针依次向后移动，累加节点个数，打印链表长度</h5></li>
<li><h5 id="代码实现如下：（2023-07-17午）"><a href="#代码实现如下：（2023-07-17午）" class="headerlink" title="代码实现如下：（2023/07/17午）"></a>代码实现如下：（2023/07/17午）</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 打印链表</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> head 头节点</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> String <span class="hljs-title function_">toString</span><span class="hljs-params">(Node head)</span> &#123;<br>        <span class="hljs-type">Node</span> <span class="hljs-variable">current</span> <span class="hljs-operator">=</span> head;<br>        <span class="hljs-type">StringBuilder</span> <span class="hljs-variable">sb</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">StringBuilder</span>();<br>        <span class="hljs-keyword">while</span> (current != <span class="hljs-literal">null</span>) &#123;<br>            sb.append(current.data).append(<span class="hljs-string">&quot;\t&quot;</span>);<br>            current = current.next;<br>        &#125;<br>        <span class="hljs-keyword">return</span> sb.toString();<br>    &#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">    * 获取链表长度</span><br><span class="hljs-comment">    *</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@param</span> head 链表头节点</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@return</span> 链表长度</span><br><span class="hljs-comment">    */</span><br>   <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-type">int</span> <span class="hljs-title function_">getLength</span><span class="hljs-params">(Node head)</span> &#123;<br>       <span class="hljs-type">int</span> <span class="hljs-variable">length</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;<br>       <span class="hljs-type">Node</span> <span class="hljs-variable">node</span> <span class="hljs-operator">=</span> head;<br>       <span class="hljs-keyword">while</span> (node != <span class="hljs-literal">null</span>) &#123;<br>           length++;<br>           node = node.next;<br>       &#125;<br>       <span class="hljs-keyword">return</span> length;<br>   &#125;<br></code></pre></td></tr></table></figure>

<h5 id="链表插入"><a href="#链表插入" class="headerlink" title="链表插入"></a>链表插入</h5><ul>
<li><h5 id="向链表中插入节点分以下三种情况："><a href="#向链表中插入节点分以下三种情况：" class="headerlink" title="向链表中插入节点分以下三种情况："></a>向链表中插入节点分以下三种情况：</h5><ol>
<li><h5 id="表头插入：创建新节点，新节点指针域指向原头节点；head指针指向新节点"><a href="#表头插入：创建新节点，新节点指针域指向原头节点；head指针指向新节点" class="headerlink" title="表头插入：创建新节点，新节点指针域指向原头节点；head指针指向新节点"></a>表头插入：创建新节点，新节点指针域指向原头节点；head指针指向新节点</h5></li>
</ol>
<p><img src="https://gitee.com/deng-2022/pictures/raw/master/images/image-20230717172246434.png" alt="image-20230717172246434"></p>
<ol>
<li><h5 id="在表中插入：遍历到插入位置的前驱节点，依次为新节点分配后继节点和前驱节点"><a href="#在表中插入：遍历到插入位置的前驱节点，依次为新节点分配后继节点和前驱节点" class="headerlink" title="在表中插入：遍历到插入位置的前驱节点，依次为新节点分配后继节点和前驱节点"></a>在表中插入：遍历到插入位置的前驱节点，依次为新节点分配后继节点和前驱节点</h5></li>
</ol>
<p><img src="https://gitee.com/deng-2022/pictures/raw/master/images/image-20230717172326686.png" alt="image-20230717172326686"></p>
<ol>
<li><h5 id="表尾插入：可视为-2-的特殊情况，新节点的后继节点为-NULL"><a href="#表尾插入：可视为-2-的特殊情况，新节点的后继节点为-NULL" class="headerlink" title="表尾插入：可视为 2 的特殊情况，新节点的后继节点为 NULL"></a>表尾插入：可视为 2 的特殊情况，新节点的后继节点为 NULL</h5></li>
</ol>
</li>
<li><h5 id="代码设计如下：（2023-07-17午）"><a href="#代码设计如下：（2023-07-17午）" class="headerlink" title="代码设计如下：（2023/07/17午）"></a>代码设计如下：（2023/07/17午）</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">    * 链表插入</span><br><span class="hljs-comment">    *</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@param</span> head       链表头节点</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@param</span> nodeInsert 待插入节点</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@param</span> position   待插入位置，取值从2开始</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@return</span> 插入后得到的链表头节点</span><br><span class="hljs-comment">    */</span><br>   <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> Node <span class="hljs-title function_">insertNode</span><span class="hljs-params">(Node head, Node nodeInsert, <span class="hljs-type">int</span> position)</span> &#123;<br>       <span class="hljs-comment">// 1.头节点判空</span><br>       <span class="hljs-keyword">if</span> (head == <span class="hljs-literal">null</span>) &#123;<br>           <span class="hljs-keyword">return</span> nodeInsert;<br>       &#125;<br>       <span class="hljs-comment">// 2.越界判断</span><br>       <span class="hljs-type">int</span> <span class="hljs-variable">size</span> <span class="hljs-operator">=</span> getLength(head);<br>       <span class="hljs-keyword">if</span> (position &gt; size + <span class="hljs-number">1</span> || position &lt; <span class="hljs-number">1</span>) &#123;<br>           System.out.println(<span class="hljs-string">&quot;位置参数越界&quot;</span>);<br>           <span class="hljs-keyword">return</span> head;<br>       &#125;<br><br>       <span class="hljs-comment">// 3.表头插入</span><br>       <span class="hljs-keyword">if</span> (position == <span class="hljs-number">1</span>) &#123;<br>           nodeInsert.next = head;<br>           head = nodeInsert;<br>           <span class="hljs-keyword">return</span> head;<br>       &#125;<br>       <span class="hljs-comment">// 4.表中/表尾插入</span><br>       <span class="hljs-type">Node</span> <span class="hljs-variable">pNode</span> <span class="hljs-operator">=</span> head;<br>       <span class="hljs-type">int</span> <span class="hljs-variable">count</span> <span class="hljs-operator">=</span> <span class="hljs-number">1</span>;<br>       <span class="hljs-keyword">while</span> (count &lt; position - <span class="hljs-number">1</span>) &#123;<br>           pNode = pNode.next;<br>           count++;<br>       &#125;<br>       nodeInsert.next = pNode.next;<br>       pNode.next = nodeInsert;<br>       <br>       <span class="hljs-comment">// 5.插入完成, 返回头节点</span><br>       <span class="hljs-keyword">return</span> head;<br>   &#125;<br></code></pre></td></tr></table></figure>

<h5 id="链表删除"><a href="#链表删除" class="headerlink" title="链表删除"></a>链表删除</h5><ul>
<li><h5 id="删除链表节点同样分三种情况："><a href="#删除链表节点同样分三种情况：" class="headerlink" title="删除链表节点同样分三种情况："></a>删除链表节点同样分三种情况：</h5><ol>
<li><h5 id="删除表头元素：head指针指向要删除节点的后继节点"><a href="#删除表头元素：head指针指向要删除节点的后继节点" class="headerlink" title="删除表头元素：head指针指向要删除节点的后继节点"></a>删除表头元素：head指针指向要删除节点的后继节点</h5></li>
</ol>
<p><img src="https://gitee.com/deng-2022/pictures/raw/master/images/image-20230717172356284.png" alt="image-20230717172356284"></p>
<ol>
<li><h5 id="删除表中元素：拿到要删除节点的前驱节点的指针域，指向要删除节点的后继节点"><a href="#删除表中元素：拿到要删除节点的前驱节点的指针域，指向要删除节点的后继节点" class="headerlink" title="删除表中元素：拿到要删除节点的前驱节点的指针域，指向要删除节点的后继节点"></a>删除表中元素：拿到要删除节点的前驱节点的指针域，指向要删除节点的后继节点</h5></li>
</ol>
<p><img src="https://gitee.com/deng-2022/pictures/raw/master/images/image-20230717172417498.png" alt="image-20230717172417498"></p>
<ol>
<li><h5 id="删除表尾元素：可视为-2-的特殊情况，要删除节点的后继节点为-NULL"><a href="#删除表尾元素：可视为-2-的特殊情况，要删除节点的后继节点为-NULL" class="headerlink" title="删除表尾元素：可视为 2 的特殊情况，要删除节点的后继节点为 NULL"></a>删除表尾元素：可视为 2 的特殊情况，要删除节点的后继节点为 NULL</h5></li>
</ol>
</li>
<li><h5 id="代码设计如下："><a href="#代码设计如下：" class="headerlink" title="代码设计如下："></a>代码设计如下：</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 删除节点</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> head     链表头节点</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> position 删除节点位置，取值从1开始</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> 删除后的链表头节点</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> Node <span class="hljs-title function_">deleteNode</span><span class="hljs-params">(Node head, <span class="hljs-type">int</span> position)</span> &#123;<br>        <span class="hljs-comment">// 1.头节点判空</span><br>        <span class="hljs-keyword">if</span> (head == <span class="hljs-literal">null</span>) &#123;<br>            <span class="hljs-keyword">return</span> <span class="hljs-literal">null</span>;<br>        &#125;<br>        <span class="hljs-comment">// 2.越界判断</span><br>        <span class="hljs-type">int</span> <span class="hljs-variable">size</span> <span class="hljs-operator">=</span> getLength(head);<br>        <span class="hljs-keyword">if</span> (position &gt; size || position &lt;= <span class="hljs-number">0</span>) &#123;<br>            System.out.println(<span class="hljs-string">&quot;输入的参数有误&quot;</span>);<br>            <span class="hljs-keyword">return</span> head;<br>        &#125;<br>        <span class="hljs-comment">// 3.表头删除</span><br>        <span class="hljs-keyword">if</span> (position == <span class="hljs-number">1</span>) &#123;<br>            <span class="hljs-comment">// head.next就是链表的新head</span><br>            head = head.next;<br>            <span class="hljs-keyword">return</span> head;<br>        &#125;<br>        <span class="hljs-comment">// 4.表中/表尾删除</span><br>        <span class="hljs-type">Node</span> <span class="hljs-variable">preNode</span> <span class="hljs-operator">=</span> head;<br>        <span class="hljs-type">int</span> <span class="hljs-variable">count</span> <span class="hljs-operator">=</span> <span class="hljs-number">1</span>;<br>        <span class="hljs-keyword">while</span> (count &lt; position - <span class="hljs-number">1</span>) &#123;<br>            preNode = preNode.next;<br>            count++;<br>        &#125;<br>        <span class="hljs-type">Node</span> <span class="hljs-variable">curNode</span> <span class="hljs-operator">=</span> preNode.next;<br>        preNode.next = curNode.next;<br>        <span class="hljs-comment">// 5.删除成功, 返回头节点</span><br>        <span class="hljs-keyword">return</span> head;<br>    &#125;<br></code></pre></td></tr></table></figure>

<h4 id="双向链表设计"><a href="#双向链表设计" class="headerlink" title="双向链表设计"></a>双向链表设计</h4><h5 id="双向链表的内部结构"><a href="#双向链表的内部结构" class="headerlink" title="双向链表的内部结构"></a>双向链表的内部结构</h5><ul>
<li><h5 id="以下是我对双向链表的理解（2023-07-17午）"><a href="#以下是我对双向链表的理解（2023-07-17午）" class="headerlink" title="以下是我对双向链表的理解（2023/07/17午）"></a>以下是我对双向链表的理解（2023/07/17午）</h5></li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs 前驱节点">双向链表与单链表的最大区别，就是每个节点增加了一个前驱指针域，指向前驱节点<br></code></pre></td></tr></table></figure>

<h5 id="链表的构造-1"><a href="#链表的构造-1" class="headerlink" title="链表的构造"></a>链表的构造</h5><ul>
<li><h5 id="代码设计如下：-1"><a href="#代码设计如下：-1" class="headerlink" title="代码设计如下："></a>代码设计如下：</h5></li>
</ul>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs"><br></code></pre></td></tr></table></figure>

<h5 id="遍历链表-1"><a href="#遍历链表-1" class="headerlink" title="遍历链表"></a>遍历链表</h5><ul>
<li><h5 id="head指针依次向后移动，遍历每个节点，输出数据域的值："><a href="#head指针依次向后移动，遍历每个节点，输出数据域的值：" class="headerlink" title="head指针依次向后移动，遍历每个节点，输出数据域的值："></a>head指针依次向后移动，遍历每个节点，输出数据域的值：</h5></li>
</ul>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs"><br></code></pre></td></tr></table></figure>

<h5 id="链表插入-1"><a href="#链表插入-1" class="headerlink" title="链表插入"></a>链表插入</h5><ul>
<li><h5 id="向链表中插入节点分以下三种情况：-1"><a href="#向链表中插入节点分以下三种情况：-1" class="headerlink" title="向链表中插入节点分以下三种情况："></a>向链表中插入节点分以下三种情况：</h5><ul>
<li><h5 id="表头插入：新建新节点，原头节点作新节点的后继节点，新节点作为原头结点的前驱节点，head指针指向新节点"><a href="#表头插入：新建新节点，原头节点作新节点的后继节点，新节点作为原头结点的前驱节点，head指针指向新节点" class="headerlink" title="表头插入：新建新节点，原头节点作新节点的后继节点，新节点作为原头结点的前驱节点，head指针指向新节点"></a>表头插入：新建新节点，原头节点作新节点的后继节点，新节点作为原头结点的前驱节点，head指针指向新节点</h5></li>
</ul>
<p><img src="https://gitee.com/deng-2022/pictures/raw/master/images/image-20230717183516334.png" alt="image-20230717183516334"></p>
<ul>
<li><h5 id="表尾插入：新建新节点，原尾节点作新节点的前驱节点，新节点作为头结点的后继节点，tail指针指向新节点"><a href="#表尾插入：新建新节点，原尾节点作新节点的前驱节点，新节点作为头结点的后继节点，tail指针指向新节点" class="headerlink" title="表尾插入：新建新节点，原尾节点作新节点的前驱节点，新节点作为头结点的后继节点，tail指针指向新节点"></a>表尾插入：新建新节点，原尾节点作新节点的前驱节点，新节点作为头结点的后继节点，tail指针指向新节点</h5></li>
</ul>
<p><img src="https://gitee.com/deng-2022/pictures/raw/master/images/image-20230717183531962.png" alt="image-20230717183531962"></p>
<ul>
<li><h5 id="表中插入"><a href="#表中插入" class="headerlink" title="表中插入"></a>表中插入</h5></li>
<li><h5 id="代码实现如下："><a href="#代码实现如下：" class="headerlink" title="代码实现如下："></a>代码实现如下：</h5></li>
</ul>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs">  <br></code></pre></td></tr></table></figure></li>
</ul>
<h5 id="链表删除-1"><a href="#链表删除-1" class="headerlink" title="链表删除"></a>链表删除</h5><ul>
<li><h5 id="删除双向链表中的节点分以下三种情况："><a href="#删除双向链表中的节点分以下三种情况：" class="headerlink" title="删除双向链表中的节点分以下三种情况："></a>删除双向链表中的节点分以下三种情况：</h5><ul>
<li><h5 id="表头删除：head指针指向原头节点的后继节点，并将该后继节点的前驱指针置空（2023-07-17午）"><a href="#表头删除：head指针指向原头节点的后继节点，并将该后继节点的前驱指针置空（2023-07-17午）" class="headerlink" title="表头删除：head指针指向原头节点的后继节点，并将该后继节点的前驱指针置空（2023/07/17午）"></a>表头删除：head指针指向原头节点的后继节点，并将该后继节点的前驱指针置空（2023/07/17午）</h5></li>
</ul>
<p><img src="https://gitee.com/deng-2022/pictures/raw/master/images/image-20230717183548124.png" alt="image-20230717183548124"></p>
<ul>
<li><h5 id="表尾删除：tail指针指向原尾节点的前驱节点，并将该前驱节点的后继指针置空"><a href="#表尾删除：tail指针指向原尾节点的前驱节点，并将该前驱节点的后继指针置空" class="headerlink" title="表尾删除：tail指针指向原尾节点的前驱节点，并将该前驱节点的后继指针置空"></a>表尾删除：tail指针指向原尾节点的前驱节点，并将该前驱节点的后继指针置空</h5></li>
</ul>
<p><img src="https://gitee.com/deng-2022/pictures/raw/master/images/image-20230717183601279.png" alt="image-20230717183601279"></p>
<ul>
<li><h5 id="表中删除"><a href="#表中删除" class="headerlink" title="表中删除"></a>表中删除</h5></li>
<li><h5 id="代码实现如下：-1"><a href="#代码实现如下：-1" class="headerlink" title="代码实现如下："></a>代码实现如下：</h5></li>
</ul>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs">  <br></code></pre></td></tr></table></figure></li>
</ul>
<h3 id="实战训练（白银挑战）"><a href="#实战训练（白银挑战）" class="headerlink" title="实战训练（白银挑战）"></a>实战训练（白银挑战）</h3><h4 id="两个链表第一个公共子节点"><a href="#两个链表第一个公共子节点" class="headerlink" title="两个链表第一个公共子节点"></a>两个链表第一个公共子节点</h4><ul>
<li><h5 id="前情提要：什么情况下，两条链表存在公共子节点呢？如下图所示：（2023-07-19午）"><a href="#前情提要：什么情况下，两条链表存在公共子节点呢？如下图所示：（2023-07-19午）" class="headerlink" title="前情提要：什么情况下，两条链表存在公共子节点呢？如下图所示：（2023/07/19午）"></a>前情提要：什么情况下，两条链表存在公共子节点呢？如下图所示：（2023/07/19午）</h5></li>
</ul>
<p><img src="https://gitee.com/deng-2022/pictures/raw/master/images/image-20230718205036232.png" alt="image-20230718205036232"></p>
<ul>
<li><h5 id="显而易见，c1、c2、c3均为两链表的公共子节点，且c1是两链表的第一个公共子节点"><a href="#显而易见，c1、c2、c3均为两链表的公共子节点，且c1是两链表的第一个公共子节点" class="headerlink" title="显而易见，c1、c2、c3均为两链表的公共子节点，且c1是两链表的第一个公共子节点"></a>显而易见，c1、c2、c3均为两链表的公共子节点，且c1是两链表的第一个公共子节点</h5></li>
<li><h5 id="我们先废话少说，给出四种解题思路："><a href="#我们先废话少说，给出四种解题思路：" class="headerlink" title="我们先废话少说，给出四种解题思路："></a>我们先废话少说，给出四种解题思路：</h5><ul>
<li><h5 id="哈希和集合，代码如下："><a href="#哈希和集合，代码如下：" class="headerlink" title="哈希和集合，代码如下："></a>哈希和集合，代码如下：</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 方法1：通过Hash辅助查找</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> pHead1 链表a</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> pHead2 链表b</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> 第一个公共节点/null</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> ListNode <span class="hljs-title function_">findFirstCommonNodeByMap</span><span class="hljs-params">(ListNode pHead1, ListNode pHead2)</span> &#123;<br>        <span class="hljs-comment">// 1.判断链表是否为空</span><br>        <span class="hljs-keyword">if</span> (pHead1 == <span class="hljs-literal">null</span> || pHead2 == <span class="hljs-literal">null</span>) &#123;<br>            <span class="hljs-keyword">return</span> <span class="hljs-literal">null</span>;<br>        &#125;<br>        <span class="hljs-comment">// 2.保存两链表头节点</span><br>        <span class="hljs-type">ListNode</span> <span class="hljs-variable">current1</span> <span class="hljs-operator">=</span> pHead1;<br>        <span class="hljs-type">ListNode</span> <span class="hljs-variable">current2</span> <span class="hljs-operator">=</span> pHead2;<br>        <span class="hljs-comment">// 3.通过Hash存储链表a的所有节点</span><br>        HashMap&lt;ListNode, Integer&gt; hashMap = <span class="hljs-keyword">new</span> <span class="hljs-title class_">HashMap</span>&lt;ListNode, Integer&gt;();<br>        <span class="hljs-keyword">while</span> (current1 != <span class="hljs-literal">null</span>) &#123;<br>            hashMap.put(current1, <span class="hljs-literal">null</span>);<br>            current1 = current1.next;<br>        &#125;<br>        <span class="hljs-comment">// 4.从头结点开始, 依次比较hash表中的节点与链表b的节点</span><br>        <span class="hljs-keyword">while</span> (current2 != <span class="hljs-literal">null</span>) &#123;<br>            <span class="hljs-keyword">if</span> (hashMap.containsKey(current2))<br>                <span class="hljs-keyword">return</span> current2;<br>            current2 = current2.next;<br>        &#125;<br>        <span class="hljs-comment">// 5.未发现公共节点</span><br>        <span class="hljs-keyword">return</span> <span class="hljs-literal">null</span>;<br>    &#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 方法2：通过集合辅助查找</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> headA 链表a</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> headB 链表b</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> 第一个公共节点/null</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> ListNode <span class="hljs-title function_">findFirstCommonNodeBySet</span><span class="hljs-params">(ListNode headA, ListNode headB)</span> &#123;<br>        <span class="hljs-comment">// 1.通过Hash存储链表a的所有节点</span><br>        Set&lt;ListNode&gt; set = <span class="hljs-keyword">new</span> <span class="hljs-title class_">HashSet</span>&lt;&gt;();<br>        <span class="hljs-keyword">while</span> (headA != <span class="hljs-literal">null</span>) &#123;<br>            set.add(headA);<br>            headA = headA.next;<br>        &#125;<br>        <span class="hljs-comment">// 2.从头结点开始, 依次比较hash表中的节点与链表b的节点</span><br>        <span class="hljs-keyword">while</span> (headB != <span class="hljs-literal">null</span>) &#123;<br>            <span class="hljs-keyword">if</span> (set.contains(headB))<br>                <span class="hljs-keyword">return</span> headB;<br>            headB = headB.next;<br>        &#125;<br>        <span class="hljs-comment">// 3.未发现公共节点</span><br>        <span class="hljs-keyword">return</span> <span class="hljs-literal">null</span>;<br>    &#125;<br></code></pre></td></tr></table></figure>

<ul>
<li><h5 id="栈，代码如下："><a href="#栈，代码如下：" class="headerlink" title="栈，代码如下："></a>栈，代码如下：</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">    * 方法3：通过栈</span><br><span class="hljs-comment">    */</span><br>   <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> ListNode <span class="hljs-title function_">findFirstCommonNodeByStack</span><span class="hljs-params">(ListNode headA, ListNode headB)</span> &#123;<br>       <span class="hljs-comment">// 1.将两条链表从头节点开始, 分别压入栈中</span><br>       Stack&lt;ListNode&gt; stackA = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Stack</span>&lt;&gt;();<br>       Stack&lt;ListNode&gt; stackB = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Stack</span>&lt;&gt;();<br>       <span class="hljs-keyword">while</span> (headA != <span class="hljs-literal">null</span>) &#123;<br>           stackA.push(headA);<br>           headA = headA.next;<br>       &#125;<br>       <span class="hljs-keyword">while</span> (headB != <span class="hljs-literal">null</span>) &#123;<br>           stackB.push(headB);<br>           headB = headB.next;<br>       &#125;<br>       <span class="hljs-comment">// 2.两栈依次出栈, 当栈顶元素相同时, 保存该元素</span><br>       <span class="hljs-type">ListNode</span> <span class="hljs-variable">preNode</span> <span class="hljs-operator">=</span> <span class="hljs-literal">null</span>;<br>       <span class="hljs-keyword">while</span> (stackB.size() &gt; <span class="hljs-number">0</span> &amp;&amp; stackA.size() &gt; <span class="hljs-number">0</span>) &#123;<br>           <span class="hljs-keyword">if</span> (stackA.peek() == stackB.peek()) &#123;<br>               preNode = stackA.pop();<br>               stackB.pop();<br>           &#125; <span class="hljs-keyword">else</span> &#123;<br>               <span class="hljs-keyword">break</span>;<br>           &#125;<br>       &#125;<br>       <span class="hljs-comment">// 3.返回第一个公共节点</span><br>       <span class="hljs-keyword">return</span> preNode;<br>   &#125;<br></code></pre></td></tr></table></figure>

<ul>
<li><h5 id="两条链表拼接，代码如下："><a href="#两条链表拼接，代码如下：" class="headerlink" title="两条链表拼接，代码如下："></a>两条链表拼接，代码如下：</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><code class="hljs java"> <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 方法4：通过序列拼接</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> ListNode <span class="hljs-title function_">findFirstCommonNodeByCombine</span><span class="hljs-params">(ListNode pHead1, ListNode pHead2)</span> &#123;<br><span class="hljs-comment">//        System.out.println(&quot;null == null&quot; + (null == null));</span><br>        <span class="hljs-comment">// 1.判断链表是否为空</span><br>        <span class="hljs-keyword">if</span> (pHead1 == <span class="hljs-literal">null</span> || pHead2 == <span class="hljs-literal">null</span>) &#123;<br>            <span class="hljs-keyword">return</span> <span class="hljs-literal">null</span>;<br>        &#125;<br><br>        <span class="hljs-type">ListNode</span> <span class="hljs-variable">p1</span> <span class="hljs-operator">=</span> pHead1;<br>        <span class="hljs-type">ListNode</span> <span class="hljs-variable">p2</span> <span class="hljs-operator">=</span> pHead2;<br>        <span class="hljs-comment">// 2.依次遍历两条链表</span><br>        <span class="hljs-keyword">while</span> (p1 != p2) &#123;<br>            p1 = p1.next;<br>            p2 = p2.next;<br>            <span class="hljs-keyword">if</span> (p1 != p2) &#123;<span class="hljs-comment">// 这个判断不能少</span><br>                <span class="hljs-comment">// 2.1.链表a遍历完, 切换遍历链表b</span><br>                <span class="hljs-keyword">if</span> (p1 == <span class="hljs-literal">null</span>) &#123;<br>                    p1 = pHead2;<br>                &#125;<br>                <span class="hljs-comment">// 2.2.链表b遍历完, 切换遍历链表a</span><br>                <span class="hljs-keyword">if</span> (p2 == <span class="hljs-literal">null</span>) &#123;<br>                    p2 = pHead1;<br>                &#125;<br>            &#125;<br>        &#125;<br>        <span class="hljs-comment">// 3.返回第一个公共节点</span><br>        <span class="hljs-keyword">return</span> p1;<br>    &#125;<br></code></pre></td></tr></table></figure>

<ul>
<li><h5 id="这里有必要解释一下这个判断的作用了："><a href="#这里有必要解释一下这个判断的作用了：" class="headerlink" title="这里有必要解释一下这个判断的作用了："></a>这里有必要解释一下这个判断的作用了：</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">if</span> (p1 == <span class="hljs-literal">null</span>) &#123;<br>	...............<br>&#125;<br></code></pre></td></tr></table></figure>

<ul>
<li><h5 id="考虑这个问题：当前链表遍历结束后，什么情况下允许切换遍历另一条链表呢？"><a href="#考虑这个问题：当前链表遍历结束后，什么情况下允许切换遍历另一条链表呢？" class="headerlink" title="考虑这个问题：当前链表遍历结束后，什么情况下允许切换遍历另一条链表呢？"></a>考虑这个问题：当前链表遍历结束后，什么情况下允许切换遍历另一条链表呢？</h5></li>
<li><h5 id="答案包括两种情况：未找到公共节点-第一次切换遍历链表结束"><a href="#答案包括两种情况：未找到公共节点-第一次切换遍历链表结束" class="headerlink" title="答案包括两种情况：未找到公共节点 / 第一次切换遍历链表结束"></a>答案包括两种情况：未找到公共节点 / 第一次切换遍历链表结束</h5></li>
<li><h5 id="未找到公共节点很好理解，只有切换遍历另一条链表，才能判断是否有公共节点"><a href="#未找到公共节点很好理解，只有切换遍历另一条链表，才能判断是否有公共节点" class="headerlink" title="未找到公共节点很好理解，只有切换遍历另一条链表，才能判断是否有公共节点"></a>未找到公共节点很好理解，只有切换遍历另一条链表，才能判断是否有公共节点</h5></li>
<li><h5 id="第一次切换遍历链表结束，此时p1、p2指针均为null，说明两链表就没有公共节点，我们就结束链表的遍历"><a href="#第一次切换遍历链表结束，此时p1、p2指针均为null，说明两链表就没有公共节点，我们就结束链表的遍历" class="headerlink" title="第一次切换遍历链表结束，此时p1、p2指针均为null，说明两链表就没有公共节点，我们就结束链表的遍历"></a>第一次切换遍历链表结束，此时p1、p2指针均为null，说明两链表就没有公共节点，我们就结束链表的遍历</h5></li>
<li><h5 id="所以结束两链表的遍历（p1-p2）有两种情况：第一个公共节点已找到-不存在公共节点"><a href="#所以结束两链表的遍历（p1-p2）有两种情况：第一个公共节点已找到-不存在公共节点" class="headerlink" title="所以结束两链表的遍历（p1 == p2）有两种情况：第一个公共节点已找到/不存在公共节点"></a>所以结束两链表的遍历（p1 == p2）有两种情况：第一个公共节点已找到/不存在公共节点</h5></li>
<li><h5 id="差和双指针"><a href="#差和双指针" class="headerlink" title="差和双指针"></a>差和双指针</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 方法5：通过差值来实现</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> pHead1 链表a</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> pHead2 链表b</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> </span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> ListNode <span class="hljs-title function_">findFirstCommonNodeBySub</span><span class="hljs-params">(ListNode pHead1, ListNode pHead2)</span> &#123;<br>        <span class="hljs-comment">// 1.判断链表是否为空</span><br>        <span class="hljs-keyword">if</span> (pHead1 == <span class="hljs-literal">null</span> || pHead2 == <span class="hljs-literal">null</span>) &#123;<br>            <span class="hljs-keyword">return</span> <span class="hljs-literal">null</span>;<br>        &#125;<br><br>        <span class="hljs-type">ListNode</span> <span class="hljs-variable">current1</span> <span class="hljs-operator">=</span> pHead1;<br>        <span class="hljs-type">ListNode</span> <span class="hljs-variable">current2</span> <span class="hljs-operator">=</span> pHead2;<br>        <span class="hljs-type">int</span> <span class="hljs-variable">l1</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>, l2 = <span class="hljs-number">0</span>;<br><br>        <span class="hljs-comment">// 2.分别拿到两链表的长度</span><br>        <span class="hljs-keyword">while</span> (current1 != <span class="hljs-literal">null</span>) &#123;<br>            current1 = current1.next;<br>            l1++;<br>        &#125;<br><br>        <span class="hljs-keyword">while</span> (current2 != <span class="hljs-literal">null</span>) &#123;<br>            current2 = current2.next;<br>            l2++;<br>        &#125;<br>        current1 = pHead1;<br>        current2 = pHead2;<br><br>        <span class="hljs-comment">// 3.计算两链表长度之差</span><br>        <span class="hljs-type">int</span> <span class="hljs-variable">sub</span> <span class="hljs-operator">=</span> l1 &gt; l2 ? l1 - l2 : l2 - l1;<br><br>        <span class="hljs-comment">// 4.长度较大的链表先遍历, 遍历次数即为长度之差</span><br>        <span class="hljs-keyword">if</span> (l1 &gt; l2) &#123;<br>            <span class="hljs-type">int</span> <span class="hljs-variable">a</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;<br>            <span class="hljs-keyword">while</span> (a &lt; sub) &#123;<br>                current1 = current1.next;<br>                a++;<br>            &#125;<br>        &#125;<br><br>        <span class="hljs-keyword">if</span> (l1 &lt; l2) &#123;<br>            <span class="hljs-type">int</span> <span class="hljs-variable">a</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;<br>            <span class="hljs-keyword">while</span> (a &lt; sub) &#123;<br>                current2 = current2.next;<br>                a++;<br>            &#125;<br>        &#125;<br><br>        <span class="hljs-comment">// 5.同时遍历两链表</span><br>        <span class="hljs-keyword">while</span> (current2 != current1) &#123;<br>            current2 = current2.next;<br>            current1 = current1.next;<br>        &#125;<br><br>        <span class="hljs-comment">// 6.返回第一个公共节点</span><br>        <span class="hljs-keyword">return</span> current1;<br>    &#125;<br></code></pre></td></tr></table></figure></li>
<li><h5 id="上面代码里的注释，已经把解题思路解释的很清晰了"><a href="#上面代码里的注释，已经把解题思路解释的很清晰了" class="headerlink" title="上面代码里的注释，已经把解题思路解释的很清晰了"></a>上面代码里的注释，已经把解题思路解释的很清晰了</h5></li>
<li><h5 id="基于我个人的理解，下面讲解一下这些方法的共同点，也就是解题思路的形成过程："><a href="#基于我个人的理解，下面讲解一下这些方法的共同点，也就是解题思路的形成过程：" class="headerlink" title="基于我个人的理解，下面讲解一下这些方法的共同点，也就是解题思路的形成过程："></a>基于我个人的理解，下面讲解一下这些方法的共同点，也就是解题思路的形成过程：</h5></li>
</ul>
<figure class="highlight awk"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs awk">我们的目标是：查出两条链表的第一个公共节点<br>公共节点是什么我们已经搞清楚了，那如何拿到第一个公共节点呢？<br>不论是分别正序/倒序遍历两条链表，我们的执行思路始终是：<br>从两链表的头节点/尾节点开始，分别依次向后遍历链表的每个节点，再比较两节点，判断它们是否相同，即是否为两链表的公共节点<br>我们能够判断出两链表的公共节点，那么第一个公共节点就好找了：<br>如果遍历顺序为正序，则选出第一组公共节点；如果遍历顺序为倒序，则选出最后一组公共节点<br>只需要根据正序<span class="hljs-regexp">/倒序遍历链表，选出第一组公共节点/</span>最后一组公共节点，就找到了两链表的第一个公共节点<br>这里问题来了，我们要明确一点，即两链表的长度不一定相同<br>这就带来了问题：<br>我们上面查找两链表公共节点的思路，其实只有在两链表长度相同时，才行得通<br>那我们的目标就是，如何构造出两链表长度相同的环境：<br>哈希和集合：直接消除了链表长度带来的影响，通过开辟了新的空间，判断节点是否相等，进而查找出两链表的公共节点<br>栈、两链表拼接、差和双指针：本质上都是构造出两链表长度相同的环境，进而查找出两链表的公共节点<br></code></pre></td></tr></table></figure>

<ul>
<li><h5 id="这就是查找两链表的第一个公共节点的解题思路了，希望对你有帮助"><a href="#这就是查找两链表的第一个公共节点的解题思路了，希望对你有帮助" class="headerlink" title="这就是查找两链表的第一个公共节点的解题思路了，希望对你有帮助"></a>这就是查找两链表的第一个公共节点的解题思路了，希望对你有帮助</h5></li>
</ul>
<h4 id="回文链表的判断"><a href="#回文链表的判断" class="headerlink" title="回文链表的判断"></a>回文链表的判断</h4><ul>
<li><h5 id="给出一个链表，判断其是否为回文链表，那什么是回文链表？"><a href="#给出一个链表，判断其是否为回文链表，那什么是回文链表？" class="headerlink" title="给出一个链表，判断其是否为回文链表，那什么是回文链表？"></a>给出一个链表，判断其是否为回文链表，那什么是回文链表？</h5></li>
<li><h5 id="以下即为一条回文链表："><a href="#以下即为一条回文链表：" class="headerlink" title="以下即为一条回文链表："></a>以下即为一条回文链表：</h5></li>
</ul>
<p><img src="https://gitee.com/deng-2022/pictures/raw/master/images/image-20230719165410932.png" alt="image-20230719165410932"></p>
<ul>
<li><h5 id="即对回文链表正序遍历和倒序遍历，得到的结果是一样的"><a href="#即对回文链表正序遍历和倒序遍历，得到的结果是一样的" class="headerlink" title="即对回文链表正序遍历和倒序遍历，得到的结果是一样的"></a>即对回文链表正序遍历和倒序遍历，得到的结果是一样的</h5></li>
<li><h5 id="这种题解法很多，我们列举常见的、简单的且容易理解的解法："><a href="#这种题解法很多，我们列举常见的、简单的且容易理解的解法：" class="headerlink" title="这种题解法很多，我们列举常见的、简单的且容易理解的解法："></a>这种题解法很多，我们列举常见的、简单的且容易理解的解法：</h5><ul>
<li><h5 id="压栈法，具体代码如下："><a href="#压栈法，具体代码如下：" class="headerlink" title="压栈法，具体代码如下："></a>压栈法，具体代码如下：</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 方法1：全部压栈遍历 全部出栈遍历</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> head</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-type">boolean</span> <span class="hljs-title function_">isPalindromeByAllStack</span><span class="hljs-params">(ListNode head)</span> &#123;<br>        <span class="hljs-type">ListNode</span> <span class="hljs-variable">temp</span> <span class="hljs-operator">=</span> head;<br>        Stack&lt;Integer&gt; stack = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Stack</span>&lt;&gt;();<br>        <span class="hljs-comment">// 1.压栈 遍历</span><br>        <span class="hljs-keyword">while</span> (temp != <span class="hljs-literal">null</span>) &#123;<br>            stack.push(temp.val);<br>            temp = temp.next;<br>        &#125;<br>        <span class="hljs-comment">// 2.出栈 遍历</span><br>        <span class="hljs-keyword">while</span> (head != <span class="hljs-literal">null</span>) &#123;<br>            <span class="hljs-keyword">if</span> (head.val != stack.pop()) &#123;<br>                <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;<br>            &#125;<br>            head = head.next;<br>        &#125;<br>        <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;<br>    &#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 方法2：全部压栈遍历 一半出栈遍历</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> head</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-type">boolean</span> <span class="hljs-title function_">isPalindromeByHalfStack</span><span class="hljs-params">(ListNode head)</span> &#123;<br>        <span class="hljs-keyword">if</span> (head == <span class="hljs-literal">null</span>)<br>            <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;<br>        <span class="hljs-type">ListNode</span> <span class="hljs-variable">temp</span> <span class="hljs-operator">=</span> head;<br>        Stack&lt;Integer&gt; stack = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Stack</span>&lt;&gt;();<br>        <span class="hljs-comment">//链表的长度</span><br>        <span class="hljs-type">int</span> <span class="hljs-variable">len</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;<br>        <span class="hljs-comment">//把链表节点的值存放到栈中</span><br>        <span class="hljs-keyword">while</span> (temp != <span class="hljs-literal">null</span>) &#123;<br>            stack.push(temp.val);<br>            temp = temp.next;<br>            len++;<br>        &#125;<br>        <span class="hljs-comment">//len长度除以2</span><br>        len &gt;&gt;= <span class="hljs-number">1</span>;<br>        <span class="hljs-comment">//然后再出栈</span><br>        <span class="hljs-keyword">while</span> (len-- &gt;= <span class="hljs-number">0</span>) &#123;<br>            <span class="hljs-keyword">if</span> (head.val != stack.pop())<br>                <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;<br>            head = head.next;<br>        &#125;<br>        <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;<br>    &#125;<br></code></pre></td></tr></table></figure>

<ul>
<li><h5 id="倒序链表法，代码如下："><a href="#倒序链表法，代码如下：" class="headerlink" title="倒序链表法，代码如下："></a>倒序链表法，代码如下：</h5></li>
<li><h5 id="根据原链表构造一条倒序链表，遍历这两条链表"><a href="#根据原链表构造一条倒序链表，遍历这两条链表" class="headerlink" title="根据原链表构造一条倒序链表，遍历这两条链表"></a>根据原链表构造一条倒序链表，遍历这两条链表</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 构造倒序链表</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> head</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-type">boolean</span> <span class="hljs-title function_">isPalindromeByReverseList</span><span class="hljs-params">(ListNode head)</span> &#123;<br>        <span class="hljs-comment">// 1.构造反转链表</span><br>        <span class="hljs-type">ListNode</span> <span class="hljs-variable">newHead</span> <span class="hljs-operator">=</span> head, temp = head;<br><br>        <span class="hljs-keyword">while</span> (temp != <span class="hljs-literal">null</span>) &#123;<br>            <span class="hljs-type">ListNode</span> <span class="hljs-variable">node</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ListNode</span>(temp.val);<br>            node.next = newHead;<br><br>            newHead = node;<br>            temp = temp.next;<br>        &#125;<br><br>        <span class="hljs-comment">// 2.同时遍历两链表</span><br>        <span class="hljs-keyword">while</span> (newHead != <span class="hljs-literal">null</span> &amp;&amp; head != <span class="hljs-literal">null</span>) &#123;<br>            <span class="hljs-keyword">if</span> (head.val != newHead.val)<br>                <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;<br><br>            head = head.next;<br>            newHead = newHead.next;<br>        &#125;<br><br>        <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;<br>    &#125;<br></code></pre></td></tr></table></figure>

<ul>
<li><h5 id="此外还有双指针法（之后双指针专题练习结束后回来补充）、递归法（不推荐掌握，容易绕晕）"><a href="#此外还有双指针法（之后双指针专题练习结束后回来补充）、递归法（不推荐掌握，容易绕晕）" class="headerlink" title="此外还有双指针法（之后双指针专题练习结束后回来补充）、递归法（不推荐掌握，容易绕晕）"></a>此外还有双指针法（之后双指针专题练习结束后回来补充）、递归法（不推荐掌握，容易绕晕）</h5></li>
</ul>
</li>
</ul>
<h4 id="合并两条有序链表"><a href="#合并两条有序链表" class="headerlink" title="合并两条有序链表"></a>合并两条有序链表</h4><ul>
<li><h5 id="常见的解法就是构造第三条链表，然后依次遍历两条有序链表，比较各节点大小，依次连接到新链表中，整个过程如下图所示："><a href="#常见的解法就是构造第三条链表，然后依次遍历两条有序链表，比较各节点大小，依次连接到新链表中，整个过程如下图所示：" class="headerlink" title="常见的解法就是构造第三条链表，然后依次遍历两条有序链表，比较各节点大小，依次连接到新链表中，整个过程如下图所示："></a>常见的解法就是构造第三条链表，然后依次遍历两条有序链表，比较各节点大小，依次连接到新链表中，整个过程如下图所示：</h5></li>
</ul>
<p><img src="https://gitee.com/deng-2022/pictures/raw/master/images/image-20230719180411312.png" alt="image-20230719180411312"></p>
<ul>
<li><h5 id="由于两条链表长度不一定相同，可能出现一条链表遍历完，另一条链表还没有的情况，这其实是一个优化点"><a href="#由于两条链表长度不一定相同，可能出现一条链表遍历完，另一条链表还没有的情况，这其实是一个优化点" class="headerlink" title="由于两条链表长度不一定相同，可能出现一条链表遍历完，另一条链表还没有的情况，这其实是一个优化点"></a>由于两条链表长度不一定相同，可能出现一条链表遍历完，另一条链表还没有的情况，这其实是一个优化点</h5></li>
<li><h5 id="具体代码如下："><a href="#具体代码如下：" class="headerlink" title="具体代码如下："></a>具体代码如下：</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 方法1：面试时就能写出来的方法</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> list1</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> list2</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> ListNode <span class="hljs-title function_">mergeTwoLists</span><span class="hljs-params">(ListNode list1, ListNode list2)</span> &#123;<br>        <span class="hljs-comment">// write code here</span><br>        <span class="hljs-type">ListNode</span> <span class="hljs-variable">newHead</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ListNode</span>(-<span class="hljs-number">1</span>);<br>        <span class="hljs-type">ListNode</span> <span class="hljs-variable">res</span> <span class="hljs-operator">=</span> newHead;<br>        <span class="hljs-keyword">while</span> (list1 != <span class="hljs-literal">null</span> || list2 != <span class="hljs-literal">null</span>) &#123;<br>			<br>            <span class="hljs-keyword">if</span> (list1 != <span class="hljs-literal">null</span> &amp;&amp; list2 != <span class="hljs-literal">null</span>) &#123;<br>                <span class="hljs-keyword">if</span> (list1.val &lt; list2.val) &#123;<br>                    newHead.next = list1;<br>                    list1 = list1.next;<br>                &#125; <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (list1.val &gt; list2.val) &#123;<br>                    newHead.next = list2;<br>                    list2 = list2.next;<br>                &#125; <span class="hljs-keyword">else</span> &#123; <span class="hljs-comment">//相等的情况，分别接两个链</span><br>                    newHead.next = list2;<br>                    list2 = list2.next;<br>                    newHead = newHead.next;<br>                    newHead.next = list1;<br>                    list1 = list1.next;<br>                &#125;<br>                newHead = newHead.next;<br>            &#125; <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (list1 != <span class="hljs-literal">null</span> &amp;&amp; list2 == <span class="hljs-literal">null</span>) &#123;<br>                newHead.next = list1;<br>                list1 = list1.next;<br>                newHead = newHead.next;<br>            &#125; <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (list1 == <span class="hljs-literal">null</span> &amp;&amp; list2 != <span class="hljs-literal">null</span>) &#123;<br>                newHead.next = list2;<br>                list2 = list2.next;<br>                newHead = newHead.next;<br>            &#125;<br>        &#125;<br>        <span class="hljs-keyword">return</span> res.next;<br>    &#125;<br></code></pre></td></tr></table></figure>

<ul>
<li><h5 id="上面的解法当中，我们把两条链表是否都为空-只有一条为空放在了一个循环下，这次我们把它拆开来："><a href="#上面的解法当中，我们把两条链表是否都为空-只有一条为空放在了一个循环下，这次我们把它拆开来：" class="headerlink" title="上面的解法当中，我们把两条链表是否都为空/只有一条为空放在了一个循环下，这次我们把它拆开来："></a>上面的解法当中，我们把两条链表是否都为空/只有一条为空放在了一个循环下，这次我们把它拆开来：</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">    * 思路更清晰的写法</span><br><span class="hljs-comment">    *</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@param</span> list1</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@param</span> list2</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">    */</span><br>   <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> ListNode <span class="hljs-title function_">mergeTwoLists2</span><span class="hljs-params">(ListNode list1, ListNode list2)</span> &#123;<br>       <span class="hljs-comment">// write code here</span><br>       <span class="hljs-type">ListNode</span> <span class="hljs-variable">newHead</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ListNode</span>(-<span class="hljs-number">1</span>);<br>       <span class="hljs-type">ListNode</span> <span class="hljs-variable">res</span> <span class="hljs-operator">=</span> newHead;<br>       <span class="hljs-comment">// 1.两链表均不为空</span><br>       <span class="hljs-keyword">while</span> (list1 != <span class="hljs-literal">null</span> &amp;&amp; list2 != <span class="hljs-literal">null</span>) &#123;<br><br>           <span class="hljs-keyword">if</span> (list1.val &lt; list2.val) &#123;<br>               newHead.next = list1;<br>               list1 = list1.next;<br>           &#125; <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (list1.val &gt; list2.val) &#123;<br>               newHead.next = list2;<br>               list2 = list2.next;<br>           &#125; <span class="hljs-keyword">else</span> &#123; <span class="hljs-comment">//相等的情况，分别接两个链</span><br>               newHead.next = list2;<br>               list2 = list2.next;<br>               newHead = newHead.next;<br>               newHead.next = list1;<br>               list1 = list1.next;<br>           &#125;<br>           newHead = newHead.next;<br><br>       &#125;<br>       <span class="hljs-comment">// 2.链表a为空</span><br>       <span class="hljs-keyword">while</span> (list1 != <span class="hljs-literal">null</span>) &#123;<br>           newHead.next = list1;<br>           list1 = list1.next;<br>           newHead = newHead.next;<br>       &#125;<br>       <span class="hljs-comment">// 3.链表b为空</span><br>       <span class="hljs-keyword">while</span> (list2 != <span class="hljs-literal">null</span>) &#123;<br>           newHead.next = list2;<br>           list2 = list2.next;<br>           newHead = newHead.next;<br>       &#125;<br><br>       <span class="hljs-keyword">return</span> res.next;<br>   &#125;<br></code></pre></td></tr></table></figure>

<ul>
<li><h5 id="思路更加清晰了，不过还有优化点："><a href="#思路更加清晰了，不过还有优化点：" class="headerlink" title="思路更加清晰了，不过还有优化点："></a>思路更加清晰了，不过还有优化点：</h5></li>
</ul>
<blockquote>
<p>一条链表合合并完成后，直接拼接剩余的另一条链表即可（2023/10/09晚）</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">    * 方法2：比方法1更加精简的实现方法</span><br><span class="hljs-comment">    *</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@param</span> l1</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@param</span> l2</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">    */</span><br>   <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> ListNode <span class="hljs-title function_">mergeTwoListsMoreSimple</span><span class="hljs-params">(ListNode l1, ListNode l2)</span> &#123;<br>       <span class="hljs-type">ListNode</span> <span class="hljs-variable">prehead</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ListNode</span>(-<span class="hljs-number">1</span>);<br>       <span class="hljs-type">ListNode</span> <span class="hljs-variable">prev</span> <span class="hljs-operator">=</span> prehead;<br>       <span class="hljs-comment">// 节点之间的比较，简化为两种情况</span><br>       <span class="hljs-keyword">while</span> (l1 != <span class="hljs-literal">null</span> &amp;&amp; l2 != <span class="hljs-literal">null</span>) &#123;<br>           <span class="hljs-keyword">if</span> (l1.val &lt;= l2.val) &#123;<br>               prev.next = l1;<br>               l1 = l1.next;<br>           &#125; <span class="hljs-keyword">else</span> &#123;<br>               prev.next = l2;<br>               l2 = l2.next;<br>           &#125;<br>           prev = prev.next;<br>       &#125;<br><br>       <span class="hljs-comment">// 一条链表合并完成，直接拼接剩余链表的节点即可</span><br>       prev.next = l1 == <span class="hljs-literal">null</span> ? l2 : l1;<br>       <span class="hljs-keyword">return</span> prehead.next;<br>   &#125;<br></code></pre></td></tr></table></figure>

<h4 id="合并K个链表"><a href="#合并K个链表" class="headerlink" title="合并K个链表"></a>合并K个链表</h4><ul>
<li><h5 id="这个更简单了，上代码："><a href="#这个更简单了，上代码：" class="headerlink" title="这个更简单了，上代码："></a>这个更简单了，上代码：</h5></li>
</ul>
<blockquote>
<p>每次两条链表合成完毕后，返回新链表的头节点，再用这条列表，继续与剩余列表合成 （2023/10/09晚）</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">    * 合并K个链表</span><br><span class="hljs-comment">    *</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@param</span> lists</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">    */</span><br>   <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> ListNode <span class="hljs-title function_">mergeKLists</span><span class="hljs-params">(ListNode[] lists)</span> &#123;<br>       <span class="hljs-type">ListNode</span> <span class="hljs-variable">res</span> <span class="hljs-operator">=</span> <span class="hljs-literal">null</span>;<br>       <span class="hljs-keyword">for</span> (ListNode list : lists) &#123;<br>           res = mergeTwoListsMoreSimple(res, list);<br>       &#125;<br>       <span class="hljs-keyword">return</span> res;<br>   &#125;<br></code></pre></td></tr></table></figure>

<h4 id="简单的合并链表"><a href="#简单的合并链表" class="headerlink" title="简单的合并链表"></a>简单的合并链表</h4><ul>
<li><h5 id="随便给你两条链表，你会怎么连接这两条链表？（比如：将链表b连接到链表a后面）"><a href="#随便给你两条链表，你会怎么连接这两条链表？（比如：将链表b连接到链表a后面）" class="headerlink" title="随便给你两条链表，你会怎么连接这两条链表？（比如：将链表b连接到链表a后面）"></a>随便给你两条链表，你会怎么连接这两条链表？（比如：将链表b连接到链表a后面）</h5></li>
<li><h5 id="正确的思路只有一个，那就是拿到链表a的尾节点，拿到链表b的头节点，作：a-next-b，连接完成"><a href="#正确的思路只有一个，那就是拿到链表a的尾节点，拿到链表b的头节点，作：a-next-b，连接完成" class="headerlink" title="正确的思路只有一个，那就是拿到链表a的尾节点，拿到链表b的头节点，作：a.next = b，连接完成"></a>正确的思路只有一个，那就是拿到链表a的尾节点，拿到链表b的头节点，作：a.next = b，连接完成</h5></li>
<li><h5 id="举个例子：将链表a的-a，b-区间删掉，把链表b连接进去，代码如下："><a href="#举个例子：将链表a的-a，b-区间删掉，把链表b连接进去，代码如下：" class="headerlink" title="举个例子：将链表a的[a，b]区间删掉，把链表b连接进去，代码如下："></a>举个例子：将链表a的[a，b]区间删掉，把链表b连接进去，代码如下：</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 简单的合并链表</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> listA</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> a</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> b</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> listB</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> ListNode <span class="hljs-title function_">mergeInBetween</span><span class="hljs-params">(ListNode listA, <span class="hljs-type">int</span> a, <span class="hljs-type">int</span> b, ListNode listB)</span> &#123;<br>        <span class="hljs-type">ListNode</span> <span class="hljs-variable">preA</span> <span class="hljs-operator">=</span> listA;<br>        <span class="hljs-type">ListNode</span> <span class="hljs-variable">postA</span> <span class="hljs-operator">=</span> listA;<br>        <span class="hljs-type">ListNode</span> <span class="hljs-variable">postB</span> <span class="hljs-operator">=</span> listB;<br><br>        <span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>, j = <span class="hljs-number">0</span>;<br><br>        <span class="hljs-keyword">while</span> (postA != <span class="hljs-literal">null</span> &amp;&amp; preA != <span class="hljs-literal">null</span> &amp;&amp; j &lt; b) &#123;<br>            <span class="hljs-comment">// 1.拿到listA的前半段preA的尾节点</span><br>            <span class="hljs-keyword">if</span> (i &lt; a - <span class="hljs-number">1</span>) &#123;<br>                preA = preA.next;<br>                i++;<br>            &#125;<br><br>            <span class="hljs-comment">// 2.拿到listA的后半段postA的头节点</span><br>            <span class="hljs-keyword">if</span> (j != b) &#123;<br>                postA = postA.next;<br>                j++;<br>            &#125;<br>        &#125;<br><br>        <span class="hljs-comment">// 3.分别连接preA与listB, postA与listB</span><br>        <span class="hljs-keyword">while</span> (postB.next != <span class="hljs-literal">null</span>) &#123;<br>            postB = postB.next;<br>        &#125;<br><br>        preA.next = listB;<br>        postB.next = postA;<br><br>        <span class="hljs-keyword">return</span> preA;<br>    &#125;<br></code></pre></td></tr></table></figure>

<h4 id="双指针"><a href="#双指针" class="headerlink" title="双指针"></a>双指针</h4><ul>
<li><h5 id="定义快慢指针（slow、fast）"><a href="#定义快慢指针（slow、fast）" class="headerlink" title="定义快慢指针（slow、fast）"></a>定义快慢指针（slow、fast）</h5></li>
</ul>
<h4 id="寻找中间节点"><a href="#寻找中间节点" class="headerlink" title="寻找中间节点"></a>寻找中间节点</h4><ul>
<li><h5 id="快慢指针均指向头节点"><a href="#快慢指针均指向头节点" class="headerlink" title="快慢指针均指向头节点"></a>快慢指针均指向头节点</h5></li>
<li><h5 id="快指针一次跳俩步，慢指针一次跳一步，两指针同时移动"><a href="#快指针一次跳俩步，慢指针一次跳一步，两指针同时移动" class="headerlink" title="快指针一次跳俩步，慢指针一次跳一步，两指针同时移动"></a>快指针一次跳俩步，慢指针一次跳一步，两指针同时移动</h5></li>
<li><h5 id="当快指针指向节点为空（偶数个节点）或快指针指向节点的后继节点为空（奇数个节点）时，两指针停止移动"><a href="#当快指针指向节点为空（偶数个节点）或快指针指向节点的后继节点为空（奇数个节点）时，两指针停止移动" class="headerlink" title="当快指针指向节点为空（偶数个节点）或快指针指向节点的后继节点为空（奇数个节点）时，两指针停止移动"></a>当快指针指向节点为空（偶数个节点）或快指针指向节点的后继节点为空（奇数个节点）时，两指针停止移动</h5></li>
<li><h5 id="此时，慢指针指向链表中间节点"><a href="#此时，慢指针指向链表中间节点" class="headerlink" title="此时，慢指针指向链表中间节点"></a>此时，慢指针指向链表中间节点</h5></li>
</ul>
<p><img src="https://gitee.com/deng-2022/pictures/raw/master/images/image-20230722225346291.png" alt="image-20230722225346291"></p>
<ul>
<li><h5 id="具体代码如下：-1"><a href="#具体代码如下：-1" class="headerlink" title="具体代码如下："></a>具体代码如下：</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 寻找中间节点</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> head</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> ListNode <span class="hljs-title function_">middleNode</span><span class="hljs-params">(ListNode head)</span> &#123;<br>        <span class="hljs-comment">// 1.快慢指针</span><br>        <span class="hljs-type">ListNode</span> <span class="hljs-variable">slow</span> <span class="hljs-operator">=</span> head, fast = head;<br>        <span class="hljs-comment">// 2.快指针指向尾节点</span><br>        <span class="hljs-keyword">while</span> (fast != <span class="hljs-literal">null</span> &amp;&amp; fast.next != <span class="hljs-literal">null</span>) &#123;<br>            slow = slow.next;<br>            fast = fast.next.next;<br>        &#125;<br>        <span class="hljs-comment">// 3.返回中间节点</span><br>        <span class="hljs-keyword">return</span> slow;<br>    &#125;<br></code></pre></td></tr></table></figure>

<h4 id="寻找倒数第K个节点"><a href="#寻找倒数第K个节点" class="headerlink" title="寻找倒数第K个节点"></a>寻找倒数第K个节点</h4><ul>
<li><h5 id="快慢指针均指向头节点-1"><a href="#快慢指针均指向头节点-1" class="headerlink" title="快慢指针均指向头节点"></a>快慢指针均指向头节点</h5></li>
<li><h5 id="快指针跳到第K-1个节点，此时慢指针与快指针相距K个节点"><a href="#快指针跳到第K-1个节点，此时慢指针与快指针相距K个节点" class="headerlink" title="快指针跳到第K+1个节点，此时慢指针与快指针相距K个节点"></a>快指针跳到第K+1个节点，此时慢指针与快指针相距K个节点</h5></li>
<li><h5 id="快慢指针同时移动，当快指针指向链表末端（即空节点）时，两指针停止移动"><a href="#快慢指针同时移动，当快指针指向链表末端（即空节点）时，两指针停止移动" class="headerlink" title="快慢指针同时移动，当快指针指向链表末端（即空节点）时，两指针停止移动"></a>快慢指针同时移动，当快指针指向链表末端（即空节点）时，两指针停止移动</h5></li>
<li><h5 id="此时，慢指针指向链表的倒数第K个节点"><a href="#此时，慢指针指向链表的倒数第K个节点" class="headerlink" title="此时，慢指针指向链表的倒数第K个节点"></a>此时，慢指针指向链表的倒数第K个节点</h5></li>
</ul>
<p><img src="https://gitee.com/deng-2022/pictures/raw/master/images/image-20230722225332136.png" alt="image-20230722225332136"></p>
<ul>
<li><h5 id="具体代码如下：-2"><a href="#具体代码如下：-2" class="headerlink" title="具体代码如下："></a>具体代码如下：</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">    * 寻找倒数第K个节点</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@param</span> head</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@param</span> k</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">    */</span><br>   <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> ListNode <span class="hljs-title function_">getKthFromEnd</span><span class="hljs-params">(ListNode head, <span class="hljs-type">int</span> k)</span> &#123;<br>       <span class="hljs-comment">// 1.快慢指针</span><br>       <span class="hljs-type">ListNode</span> <span class="hljs-variable">fast</span> <span class="hljs-operator">=</span> head;<br>       <span class="hljs-type">ListNode</span> <span class="hljs-variable">slow</span> <span class="hljs-operator">=</span> head;<br>       <span class="hljs-comment">// 2.快指针指向 K+1</span><br>       <span class="hljs-keyword">while</span> (fast != <span class="hljs-literal">null</span> &amp;&amp; k &gt; <span class="hljs-number">0</span>) &#123;<br>           fast = fast.next;<br>           k--;<br>       &#125;<br>       <span class="hljs-comment">// 3.快指针指向链表末</span><br>       <span class="hljs-keyword">while</span> (fast != <span class="hljs-literal">null</span>) &#123;<br>           fast = fast.next;<br>           slow = slow.next;<br>       &#125;<br>       <span class="hljs-comment">// 4.返回倒数第K节点</span><br>       <span class="hljs-keyword">return</span> slow;<br>   &#125;<br></code></pre></td></tr></table></figure>

<ul>
<li><h5 id="寻找倒数第-K-个节点还有两种方法：遍历链表法和压栈法"><a href="#寻找倒数第-K-个节点还有两种方法：遍历链表法和压栈法" class="headerlink" title="寻找倒数第 K 个节点还有两种方法：遍历链表法和压栈法"></a>寻找倒数第 K 个节点还有两种方法：遍历链表法和压栈法</h5></li>
<li><h5 id="遍历链表：先遍历一遍链表，得到链表长度-L，再遍历一遍链表，取第-L-K-1个节点"><a href="#遍历链表：先遍历一遍链表，得到链表长度-L，再遍历一遍链表，取第-L-K-1个节点" class="headerlink" title="遍历链表：先遍历一遍链表，得到链表长度 L，再遍历一遍链表，取第 L-K+1个节点"></a>遍历链表：先遍历一遍链表，得到链表长度 L，再遍历一遍链表，取第 L-K+1个节点</h5></li>
<li><h5 id="压栈：将链表压入栈，再出栈，取第-K-个出栈的节点"><a href="#压栈：将链表压入栈，再出栈，取第-K-个出栈的节点" class="headerlink" title="压栈：将链表压入栈，再出栈，取第 K 个出栈的节点"></a>压栈：将链表压入栈，再出栈，取第 K 个出栈的节点</h5></li>
<li><h5 id="这两种方法很好理解，具体代码择日实现（2023-07-24晚）"><a href="#这两种方法很好理解，具体代码择日实现（2023-07-24晚）" class="headerlink" title="这两种方法很好理解，具体代码择日实现（2023/07/24晚）"></a>这两种方法很好理解，具体代码择日实现（2023/07/24晚）</h5></li>
</ul>
<h4 id="旋转链表"><a href="#旋转链表" class="headerlink" title="旋转链表"></a>旋转链表</h4><ul>
<li><h5 id="常见的情景题：把链表的每个节点，都向右移动K个位置"><a href="#常见的情景题：把链表的每个节点，都向右移动K个位置" class="headerlink" title="常见的情景题：把链表的每个节点，都向右移动K个位置"></a>常见的情景题：把链表的每个节点，都向右移动K个位置</h5></li>
<li><h5 id="这个是有两种思路的：反转链表、转化为寻找倒数第-K-1-个节点"><a href="#这个是有两种思路的：反转链表、转化为寻找倒数第-K-1-个节点" class="headerlink" title="这个是有两种思路的：反转链表、转化为寻找倒数第 K-1 个节点"></a>这个是有两种思路的：反转链表、转化为寻找倒数第 K-1 个节点</h5></li>
<li><h5 id="反转链表暂且不表，这里可以看看第二种方法：转化为寻找倒数第-K-1-个节点"><a href="#反转链表暂且不表，这里可以看看第二种方法：转化为寻找倒数第-K-1-个节点" class="headerlink" title="反转链表暂且不表，这里可以看看第二种方法：转化为寻找倒数第 K-1 个节点"></a>反转链表暂且不表，这里可以看看第二种方法：转化为寻找倒数第 K-1 个节点</h5></li>
<li><h5 id="把链表的每个节点，都向右移动K个位置-gt-把链表的后-K-个节点，都旋转成前-K-个节点"><a href="#把链表的每个节点，都向右移动K个位置-gt-把链表的后-K-个节点，都旋转成前-K-个节点" class="headerlink" title="把链表的每个节点，都向右移动K个位置 =&gt; 把链表的后 K 个节点，都旋转成前 K 个节点"></a>把链表的每个节点，都向右移动K个位置 =&gt; 把链表的后 K 个节点，都旋转成前 K 个节点</h5></li>
<li><h5 id="那就把问题转换成了：转化为寻找倒数第-K-1-个节点："><a href="#那就把问题转换成了：转化为寻找倒数第-K-1-个节点：" class="headerlink" title="那就把问题转换成了：转化为寻找倒数第 K-1 个节点："></a>那就把问题转换成了：转化为寻找倒数第 K-1 个节点：</h5></li>
<li><h5 id="此时慢指针指向了倒数第-K-1-个节点，快指针指向了链表的尾节点"><a href="#此时慢指针指向了倒数第-K-1-个节点，快指针指向了链表的尾节点" class="headerlink" title="此时慢指针指向了倒数第 K-1 个节点，快指针指向了链表的尾节点"></a>此时慢指针指向了倒数第 K-1 个节点，快指针指向了链表的尾节点</h5></li>
<li><h5 id="倒数第-K-个节点为头节点（断掉慢指针指向节点的后继，快指针指向原头节点）"><a href="#倒数第-K-个节点为头节点（断掉慢指针指向节点的后继，快指针指向原头节点）" class="headerlink" title="倒数第 K 个节点为头节点（断掉慢指针指向节点的后继，快指针指向原头节点）"></a>倒数第 K 个节点为头节点（断掉慢指针指向节点的后继，快指针指向原头节点）</h5></li>
</ul>
<p><img src="https://gitee.com/deng-2022/pictures/raw/master/images/image-20230722225315216.png" alt="image-20230722225315216"></p>
<ul>
<li><h5 id="具体代码如下：-3"><a href="#具体代码如下：-3" class="headerlink" title="具体代码如下："></a>具体代码如下：</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">    * 旋转链表</span><br><span class="hljs-comment">    *</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@param</span> head</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@param</span> k</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">    */</span><br>   <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> ListNode <span class="hljs-title function_">rotateRight</span><span class="hljs-params">(ListNode head, <span class="hljs-type">int</span> k)</span> &#123;<br>       <span class="hljs-keyword">if</span> (head == <span class="hljs-literal">null</span> || k == <span class="hljs-number">0</span>) &#123;<br>           <span class="hljs-keyword">return</span> head;<br>       &#125;<br>       <span class="hljs-comment">// 1.快慢节点</span><br>       <span class="hljs-type">ListNode</span> <span class="hljs-variable">temp</span> <span class="hljs-operator">=</span> head;<br>       <span class="hljs-type">ListNode</span> <span class="hljs-variable">fast</span> <span class="hljs-operator">=</span> head;<br>       <span class="hljs-type">ListNode</span> <span class="hljs-variable">slow</span> <span class="hljs-operator">=</span> head;<br>       <span class="hljs-comment">// 2.获取链表长度</span><br>       <span class="hljs-type">int</span> <span class="hljs-variable">len</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;<br>       <span class="hljs-keyword">while</span> (head != <span class="hljs-literal">null</span>) &#123;<br>           head = head.next;<br>           len++;<br>       &#125;<br>       <span class="hljs-comment">// 3.以首尾旋转</span><br>       <span class="hljs-keyword">if</span> (k % len == <span class="hljs-number">0</span>) &#123;<br>           <span class="hljs-keyword">return</span> temp;<br>       &#125;<br>       <span class="hljs-comment">// 4.快指针先走K步</span><br>       <span class="hljs-keyword">while</span> ((k % len) &gt; <span class="hljs-number">0</span>) &#123;<br>           k--;<br>           fast = fast.next;<br>       &#125;<br>       <span class="hljs-comment">// 5.快慢指针同时走</span><br>       <span class="hljs-keyword">while</span> (fast.next != <span class="hljs-literal">null</span>) &#123;<br>           fast = fast.next;<br>           slow = slow.next;<br>       &#125;<br>       <span class="hljs-comment">// 6.获得截断处</span><br>       <span class="hljs-type">ListNode</span> <span class="hljs-variable">res</span> <span class="hljs-operator">=</span> slow.next;<br>       slow.next = <span class="hljs-literal">null</span>;<br>       <span class="hljs-comment">// 7.重置头节点</span><br>       fast.next = temp;<br>       <span class="hljs-keyword">return</span> res;<br>   &#125;<br></code></pre></td></tr></table></figure>

<h4 id="删除特定节点"><a href="#删除特定节点" class="headerlink" title="删除特定节点"></a>删除特定节点</h4><ul>
<li><h5 id="这类型题目本身不难，因为我们之前学过删除节点，但删除节点有两种情况：删除头节点和删除尾节点"><a href="#这类型题目本身不难，因为我们之前学过删除节点，但删除节点有两种情况：删除头节点和删除尾节点" class="headerlink" title="这类型题目本身不难，因为我们之前学过删除节点，但删除节点有两种情况：删除头节点和删除尾节点"></a>这类型题目本身不难，因为我们之前学过删除节点，但删除节点有两种情况：删除头节点和删除尾节点</h5></li>
<li><h5 id="这两种情况的处理方式是不一样的，所以我们提供一个全新的思路："><a href="#这两种情况的处理方式是不一样的，所以我们提供一个全新的思路：" class="headerlink" title="这两种情况的处理方式是不一样的，所以我们提供一个全新的思路："></a>这两种情况的处理方式是不一样的，所以我们提供一个全新的思路：</h5><ul>
<li><h5 id="创建虚拟头节点，消除被删节点可能为头节点的情况"><a href="#创建虚拟头节点，消除被删节点可能为头节点的情况" class="headerlink" title="创建虚拟头节点，消除被删节点可能为头节点的情况"></a>创建虚拟头节点，消除被删节点可能为头节点的情况</h5></li>
</ul>
</li>
</ul>
<ul>
<li><h5 id="具体代码如下：-4"><a href="#具体代码如下：-4" class="headerlink" title="具体代码如下："></a>具体代码如下：</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">    * 删除特定值的结点</span><br><span class="hljs-comment">    *</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@param</span> head</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@param</span> val</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">    */</span><br>   <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> ListNode <span class="hljs-title function_">removeElements</span><span class="hljs-params">(ListNode head, <span class="hljs-type">int</span> val)</span> &#123;<br>       <span class="hljs-comment">// 虚拟头节点</span><br>       <span class="hljs-type">ListNode</span> <span class="hljs-variable">dummyHead</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ListNode</span>(<span class="hljs-number">0</span>);<br>       dummyHead.next = head;<br>       <span class="hljs-comment">// 向后遍历，删除指定节点</span><br>       <span class="hljs-type">ListNode</span> <span class="hljs-variable">temp</span> <span class="hljs-operator">=</span> dummyHead;<br>       <span class="hljs-keyword">while</span> (temp.next != <span class="hljs-literal">null</span>) &#123;<br>           <span class="hljs-keyword">if</span> (temp.next.val == val) &#123;<br>               temp.next = temp.next.next;<br>           &#125; <span class="hljs-keyword">else</span> &#123;<br>               temp = temp.next;<br>           &#125;<br>       &#125;<br>       <span class="hljs-comment">// 返回头节点</span><br>       <span class="hljs-keyword">return</span> dummyHead.next;<br>   &#125;<br></code></pre></td></tr></table></figure>

<h4 id="删除倒数第K个节点"><a href="#删除倒数第K个节点" class="headerlink" title="删除倒数第K个节点"></a>删除倒数第K个节点</h4><ul>
<li><h5 id="我们之前学过如何查找倒数第K个节点，它们本质上是一样的，我们还是提供三种思路："><a href="#我们之前学过如何查找倒数第K个节点，它们本质上是一样的，我们还是提供三种思路：" class="headerlink" title="我们之前学过如何查找倒数第K个节点，它们本质上是一样的，我们还是提供三种思路："></a>我们之前学过如何查找倒数第K个节点，它们本质上是一样的，我们还是提供三种思路：</h5><ul>
<li><h5 id="遍历链表法"><a href="#遍历链表法" class="headerlink" title="遍历链表法"></a>遍历链表法</h5></li>
<li><h5 id="压栈法"><a href="#压栈法" class="headerlink" title="压栈法"></a>压栈法</h5></li>
<li><h5 id="双指针法"><a href="#双指针法" class="headerlink" title="双指针法"></a>双指针法</h5></li>
</ul>
</li>
<li><h5 id="具体代码如下：-5"><a href="#具体代码如下：-5" class="headerlink" title="具体代码如下："></a>具体代码如下：</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">    * 方法1：遍历链表法</span><br><span class="hljs-comment">    *</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@param</span> head</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@param</span> n</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">    */</span><br>   <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> ListNode <span class="hljs-title function_">removeNthFromEndByLength</span><span class="hljs-params">(ListNode head, <span class="hljs-type">int</span> n)</span> &#123;<br>       <span class="hljs-comment">// 虚拟头节点</span><br>       <span class="hljs-type">ListNode</span> <span class="hljs-variable">dummy</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ListNode</span>(<span class="hljs-number">0</span>);<br>       dummy.next = head;<br>       <span class="hljs-comment">// 获取链表长度</span><br>       <span class="hljs-type">int</span> <span class="hljs-variable">length</span> <span class="hljs-operator">=</span> getLength(head);<br>       <span class="hljs-type">ListNode</span> <span class="hljs-variable">cur</span> <span class="hljs-operator">=</span> dummy;<br>       <span class="hljs-comment">// 删除第L-n+1个节点</span><br>       <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">1</span>; i &lt; length - n + <span class="hljs-number">1</span>; ++i) &#123;<br>           cur = cur.next;<br>       &#125;<br>       cur.next = cur.next.next;<br>       <span class="hljs-comment">// 返回头节点</span><br>       <span class="hljs-keyword">return</span> dummy.next;<br>   &#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">   * 方法2：压栈法</span><br><span class="hljs-comment">   *</span><br><span class="hljs-comment">   * <span class="hljs-doctag">@param</span> head</span><br><span class="hljs-comment">   * <span class="hljs-doctag">@param</span> n</span><br><span class="hljs-comment">   * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">   */</span><br>  <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> ListNode <span class="hljs-title function_">removeNthFromEndByStack</span><span class="hljs-params">(ListNode head, <span class="hljs-type">int</span> n)</span> &#123;<br>      <span class="hljs-comment">// 虚拟头节点</span><br>      <span class="hljs-type">ListNode</span> <span class="hljs-variable">dummy</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ListNode</span>(<span class="hljs-number">0</span>);<br>      dummy.next = head;<br>      <span class="hljs-comment">// 栈</span><br>      Deque&lt;ListNode&gt; stack = <span class="hljs-keyword">new</span> <span class="hljs-title class_">LinkedList</span>&lt;ListNode&gt;();<br>      <span class="hljs-comment">// 全部压入栈</span><br>      <span class="hljs-type">ListNode</span> <span class="hljs-variable">cur</span> <span class="hljs-operator">=</span> dummy;<br>      <span class="hljs-keyword">while</span> (cur != <span class="hljs-literal">null</span>) &#123;<br>          stack.push(cur);<br>          cur = cur.next;<br>      &#125;<br>      <span class="hljs-comment">// 依次出栈，删除第n个节点</span><br>      <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i &lt; n; ++i) &#123;<br>          stack.pop();<br>      &#125;<br>      <span class="hljs-type">ListNode</span> <span class="hljs-variable">prev</span> <span class="hljs-operator">=</span> stack.peek();<br>      <span class="hljs-keyword">assert</span> prev != <span class="hljs-literal">null</span>;<br>      prev.next = prev.next.next;<br>      <span class="hljs-comment">// 返回头节点</span><br>      <span class="hljs-keyword">return</span> dummy.next;<br>  &#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">    * 方法3：双指针法</span><br><span class="hljs-comment">    *</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@param</span> head</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@param</span> n</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">    */</span><br>   <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> ListNode <span class="hljs-title function_">removeNthFromEndByTwoPoints</span><span class="hljs-params">(ListNode head, <span class="hljs-type">int</span> n)</span> &#123;<br>       <span class="hljs-comment">// 虚拟头节点</span><br>       <span class="hljs-type">ListNode</span> <span class="hljs-variable">dummy</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ListNode</span>(<span class="hljs-number">0</span>);<br>       dummy.next = head;<br>       <span class="hljs-comment">// 快慢指针</span><br>       <span class="hljs-type">ListNode</span> <span class="hljs-variable">first</span> <span class="hljs-operator">=</span> head;<br>       <span class="hljs-type">ListNode</span> <span class="hljs-variable">second</span> <span class="hljs-operator">=</span> dummy;<br>       <span class="hljs-comment">// 快指针先走n步</span><br>       <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i &lt; n; ++i) &#123;<br>           first = first.next;<br>       &#125;<br>       <span class="hljs-comment">// 快慢指针同时走</span><br>       <span class="hljs-keyword">while</span> (first != <span class="hljs-literal">null</span>) &#123;<br>           first = first.next;<br>           second = second.next;<br>       &#125;<br>       <span class="hljs-comment">// 删除节点</span><br>       <span class="hljs-keyword">assert</span> second.next != <span class="hljs-literal">null</span>;<br>       second.next = second.next.next;<br>       <span class="hljs-keyword">return</span> dummy.next;<br>   &#125;<br></code></pre></td></tr></table></figure>

<h4 id="删除重复节点"><a href="#删除重复节点" class="headerlink" title="删除重复节点"></a>删除重复节点</h4><ul>
<li><h5 id="删除重复节点当然有两种情况了：仅留一个或者删除全部，废话少说，直接上代码："><a href="#删除重复节点当然有两种情况了：仅留一个或者删除全部，废话少说，直接上代码：" class="headerlink" title="删除重复节点当然有两种情况了：仅留一个或者删除全部，废话少说，直接上代码："></a>删除重复节点当然有两种情况了：仅留一个或者删除全部，废话少说，直接上代码：</h5><ul>
<li>重复元素保留一个，在这种情况下，<strong>被删除的节点肯定不可能是头节点</strong>（头节点删除时的处理是不一样的）<strong>2023/11/03晚</strong></li>
<li>所以此处不用考虑删除节点是头节点还是中间节点了</li>
</ul>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 重复元素保留一个</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> head</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> ListNode <span class="hljs-title function_">deleteDuplicate</span><span class="hljs-params">(ListNode head)</span> &#123;<br>        <span class="hljs-keyword">if</span> (head == <span class="hljs-literal">null</span>) &#123;<br>            <span class="hljs-keyword">return</span> head;<br>        &#125;<br>        <span class="hljs-comment">// 删除重复元素</span><br>        <span class="hljs-type">ListNode</span> <span class="hljs-variable">cur</span> <span class="hljs-operator">=</span> head;<br>        <span class="hljs-keyword">while</span> (cur.next != <span class="hljs-literal">null</span>) &#123;<br>            <span class="hljs-keyword">if</span> (cur.val == cur.next.val) &#123;<br>                cur.next = cur.next.next;<br>            &#125; <span class="hljs-keyword">else</span> &#123;<br>                cur = cur.next;<br>            &#125;<br>        &#125;<br>        <span class="hljs-comment">// 返回头节点</span><br>        <span class="hljs-keyword">return</span> head;<br>    &#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 重复元素都不要</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> head</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> ListNode <span class="hljs-title function_">deleteDuplicates</span><span class="hljs-params">(ListNode head)</span> &#123;<br>        <span class="hljs-keyword">if</span> (head == <span class="hljs-literal">null</span>) &#123;<br>            <span class="hljs-keyword">return</span> <span class="hljs-literal">null</span>;<br>        &#125;<br>        <br>        <span class="hljs-comment">//虚拟节点</span><br>        <span class="hljs-type">ListNode</span> <span class="hljs-variable">dummy</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ListNode</span>(<span class="hljs-number">0</span>);<br>        dummy.next = head;<br>        <span class="hljs-type">ListNode</span> <span class="hljs-variable">cur</span> <span class="hljs-operator">=</span> dummy;<br>        <span class="hljs-comment">// 找到重复元素</span><br>        <span class="hljs-keyword">while</span> (cur.next != <span class="hljs-literal">null</span> &amp;&amp; cur.next.next != <span class="hljs-literal">null</span>) &#123;<br>            <span class="hljs-comment">// 删除所有重复元素</span><br>            <span class="hljs-keyword">if</span> (cur.next.val == cur.next.next.val) &#123;<br>                <span class="hljs-type">int</span> <span class="hljs-variable">x</span> <span class="hljs-operator">=</span> cur.next.val;<br>                <span class="hljs-keyword">while</span> (cur.next != <span class="hljs-literal">null</span> &amp;&amp; cur.next.val == x) &#123;<br>                    cur.next = cur.next.next;<br>                &#125;<br>            &#125; <span class="hljs-keyword">else</span> &#123;<br>                cur = cur.next;<br>            &#125;<br>        &#125;<br>        <span class="hljs-comment">// 返回头节点</span><br>        <span class="hljs-keyword">return</span> dummy.next;<br>    &#125;<br></code></pre></td></tr></table></figure>

<h3 id="通关（过关挑战）"><a href="#通关（过关挑战）" class="headerlink" title="通关（过关挑战）"></a>通关（过关挑战）</h3><ul>
<li><h5 id="题目内容："><a href="#题目内容：" class="headerlink" title="题目内容："></a>题目内容：</h5></li>
<li><h5 id="算法训练营开课了，小伙伴们踊跃报名，请用链表来帮忙统计学员信息："><a href="#算法训练营开课了，小伙伴们踊跃报名，请用链表来帮忙统计学员信息：" class="headerlink" title="算法训练营开课了，小伙伴们踊跃报名，请用链表来帮忙统计学员信息："></a>算法训练营开课了，小伙伴们踊跃报名，请用链表来帮忙统计学员信息：</h5></li>
<li><h5 id="学院方向不同：Java、Python、C-，仅有一条链表，其前中后三部分，分别是：Java、Python、C-的同学"><a href="#学院方向不同：Java、Python、C-，仅有一条链表，其前中后三部分，分别是：Java、Python、C-的同学" class="headerlink" title="学院方向不同：Java、Python、C++，仅有一条链表，其前中后三部分，分别是：Java、Python、C++的同学"></a>学院方向不同：Java、Python、C++，仅有一条链表，其前中后三部分，分别是：Java、Python、C++的同学</h5></li>
<li><h5 id="每种语言都会不断有学生进来，每次都要将对应的同学插入到对应的段的末尾"><a href="#每种语言都会不断有学生进来，每次都要将对应的同学插入到对应的段的末尾" class="headerlink" title="每种语言都会不断有学生进来，每次都要将对应的同学插入到对应的段的末尾"></a>每种语言都会不断有学生进来，每次都要将对应的同学插入到对应的段的末尾</h5></li>
<li><h5 id="具体代码如下：（2023-07-27早）"><a href="#具体代码如下：（2023-07-27早）" class="headerlink" title="具体代码如下：（2023/07/27早）"></a>具体代码如下：（2023/07/27早）</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@author</span> 邓哈哈</span><br><span class="hljs-comment"> * 2023/7/27 9:17</span><br><span class="hljs-comment"> * Function:</span><br><span class="hljs-comment"> * Version 1.0</span><br><span class="hljs-comment"> */</span><br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">InsertStudent</span> &#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> &#123;<br>        <span class="hljs-type">ListNode</span> <span class="hljs-variable">node1</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ListNode</span>(<span class="hljs-string">&quot;Node 1&quot;</span>, <span class="hljs-string">&quot;Java&quot;</span>);<br>        <span class="hljs-type">ListNode</span> <span class="hljs-variable">node2</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ListNode</span>(<span class="hljs-string">&quot;Node 2&quot;</span>, <span class="hljs-string">&quot;Python&quot;</span>);<br>        <span class="hljs-type">ListNode</span> <span class="hljs-variable">node3</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ListNode</span>(<span class="hljs-string">&quot;Node 3&quot;</span>, <span class="hljs-string">&quot;C++&quot;</span>);<br>        <span class="hljs-comment">// 创建节点数组并存储节点</span><br>        ListNode[] nodes = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ListNode</span>[<span class="hljs-number">3</span>];<br>        nodes[<span class="hljs-number">0</span>] = node1;<br>        nodes[<span class="hljs-number">1</span>] = node2;<br>        nodes[<span class="hljs-number">2</span>] = node3;<br>        <span class="hljs-comment">// 初始化链表</span><br>        <span class="hljs-type">ListNode</span> <span class="hljs-variable">head</span> <span class="hljs-operator">=</span> initLinkList(nodes);<br><br>        <span class="hljs-type">ListNode</span> <span class="hljs-variable">node4</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ListNode</span>(<span class="hljs-string">&quot;Node 4&quot;</span>, <span class="hljs-string">&quot;Java&quot;</span>);<br>        <span class="hljs-type">ListNode</span> <span class="hljs-variable">node5</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ListNode</span>(<span class="hljs-string">&quot;Node 5&quot;</span>, <span class="hljs-string">&quot;C++&quot;</span>);<br>        <span class="hljs-type">ListNode</span> <span class="hljs-variable">node6</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ListNode</span>(<span class="hljs-string">&quot;Node 6&quot;</span>, <span class="hljs-string">&quot;Python&quot;</span>);<br>        <span class="hljs-type">ListNode</span> <span class="hljs-variable">node8</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ListNode</span>(<span class="hljs-string">&quot;Node 8&quot;</span>, <span class="hljs-string">&quot;C++&quot;</span>);<br>        <span class="hljs-type">ListNode</span> <span class="hljs-variable">node9</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ListNode</span>(<span class="hljs-string">&quot;Node 9&quot;</span>, <span class="hljs-string">&quot;Python&quot;</span>);<br>        <span class="hljs-type">ListNode</span> <span class="hljs-variable">node7</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ListNode</span>(<span class="hljs-string">&quot;Node 7&quot;</span>, <span class="hljs-string">&quot;Java&quot;</span>);<br>        <span class="hljs-comment">// 插入学生节点</span><br>        insertStudentByLanguage(node4, head);<br>        insertStudentByLanguage(node5, head);<br>        insertStudentByLanguage(node6, head);<br>        insertStudentByLanguage(node7, head);<br>        insertStudentByLanguage(node8, head);<br>        insertStudentByLanguage(node9, head);<br><br>        printLinkList(head);<br>    &#125;<br>    <span class="hljs-comment">// 插入学生节点</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">insertStudentByLanguage</span><span class="hljs-params">(ListNode node, ListNode head)</span> &#123;<br>        <span class="hljs-type">ListNode</span> <span class="hljs-variable">cur</span> <span class="hljs-operator">=</span> head;<br>        <span class="hljs-type">String</span> <span class="hljs-variable">language</span> <span class="hljs-operator">=</span> node.language;<br><br>        <span class="hljs-keyword">switch</span> (language) &#123;<br>            <span class="hljs-keyword">case</span> <span class="hljs-string">&quot;Java&quot;</span>:<br>                <span class="hljs-keyword">while</span> (!cur.next.language.equals(<span class="hljs-string">&quot;Python&quot;</span>)) &#123;<br>                    cur = cur.next;<br>                &#125;<br>                node.next = cur.next;<br>                cur.next = node;<br>                <span class="hljs-keyword">break</span>;<br>            <span class="hljs-keyword">case</span> <span class="hljs-string">&quot;Python&quot;</span>:<br>                <span class="hljs-keyword">while</span> (!cur.next.language.equals(<span class="hljs-string">&quot;C++&quot;</span>)) &#123;<br>                    cur = cur.next;<br>                &#125;<br>                node.next = cur.next;<br>                cur.next = node;<br>                <span class="hljs-keyword">break</span>;<br>            <span class="hljs-keyword">case</span> <span class="hljs-string">&quot;C++&quot;</span>:<br>                <span class="hljs-keyword">while</span> (cur.next != <span class="hljs-literal">null</span>) &#123;<br>                    cur = cur.next;<br>                &#125;<br>                cur.next = node;<br>                <span class="hljs-keyword">break</span>;<br>            <span class="hljs-keyword">default</span>:<br>                <span class="hljs-keyword">break</span>;<br>        &#125;<br>    &#125;<br>    <span class="hljs-comment">// 打印链表</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">printLinkList</span><span class="hljs-params">(ListNode head)</span> &#123;<br>        <span class="hljs-type">ListNode</span> <span class="hljs-variable">temp</span> <span class="hljs-operator">=</span> head;<br>        <span class="hljs-keyword">while</span> (temp != <span class="hljs-literal">null</span>) &#123;<br>            System.out.println(temp + <span class="hljs-string">&quot;--&gt; &quot;</span>);<br>            temp = temp.next;<br>        &#125;<br>    &#125;<br>    <span class="hljs-comment">// 初始化链表</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> ListNode <span class="hljs-title function_">initLinkList</span><span class="hljs-params">(ListNode[] array)</span> &#123;<br>        <span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;<br>        <span class="hljs-type">ListNode</span> <span class="hljs-variable">head</span> <span class="hljs-operator">=</span> <span class="hljs-literal">null</span>, cur = <span class="hljs-literal">null</span>;<br>        <span class="hljs-keyword">while</span> (i &lt; array.length) &#123;<br>            <span class="hljs-type">ListNode</span> <span class="hljs-variable">newNode</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ListNode</span>(array[i].name, array[i].language);<br><br>            <span class="hljs-keyword">if</span> (head == <span class="hljs-literal">null</span>) &#123;<br>                head = newNode;<br>                cur = head;<br>            &#125; <span class="hljs-keyword">else</span> &#123;<br>                cur.next = newNode;<br>                cur = newNode;<br>            &#125;<br>            i++;<br>        &#125;<br>        <span class="hljs-keyword">return</span> head;<br>    &#125;<br><br>    <span class="hljs-comment">// 节点结构</span><br>    <span class="hljs-keyword">static</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">ListNode</span> &#123;<br>        <span class="hljs-keyword">public</span> String name;<br>        <span class="hljs-keyword">public</span> String language;<br>        <span class="hljs-keyword">public</span> ListNode next;<br>        <span class="hljs-keyword">public</span> <span class="hljs-title function_">ListNode</span><span class="hljs-params">(String name, String language)</span> &#123;<br>            <span class="hljs-built_in">this</span>.name = name;<br>            <span class="hljs-built_in">this</span>.language = language;<br>        &#125;<br>        <span class="hljs-meta">@Override</span><br>        <span class="hljs-keyword">public</span> String <span class="hljs-title function_">toString</span><span class="hljs-params">()</span> &#123;<br>            <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;ListNode&#123;&quot;</span> +<br>                    <span class="hljs-string">&quot;name=&#x27;&quot;</span> + name + <span class="hljs-string">&#x27;\&#x27;&#x27;</span> +<br>                    <span class="hljs-string">&quot;, language=&#x27;&quot;</span> + language + <span class="hljs-string">&#x27;\&#x27;&#x27;</span> +<br>                    <span class="hljs-string">&#x27;&#125;&#x27;</span>;<br>        &#125;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h2 id="反转链表"><a href="#反转链表" class="headerlink" title="反转链表"></a>反转链表</h2><h3 id="基础知识-基本操作（青铜挑战）"><a href="#基础知识-基本操作（青铜挑战）" class="headerlink" title="基础知识 + 基本操作（青铜挑战）"></a>基础知识 + 基本操作（青铜挑战）</h3><ul>
<li><h5 id="什么是链表反转呢？如图所示："><a href="#什么是链表反转呢？如图所示：" class="headerlink" title="什么是链表反转呢？如图所示："></a>什么是链表反转呢？如图所示：</h5></li>
</ul>
<p><img src="https://gitee.com/deng-2022/pictures/raw/master/images/image-20230728093653981.png" alt="image-20230728093653981"></p>
<ul>
<li><p>常见的链表反转的方法：虚拟头节点、直接反转、递归</p>
</li>
<li><h5 id="虚拟头节点，具体代码如下："><a href="#虚拟头节点，具体代码如下：" class="headerlink" title="虚拟头节点，具体代码如下："></a>虚拟头节点，具体代码如下：</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">   * 方法1：虚拟头结点</span><br><span class="hljs-comment">   *</span><br><span class="hljs-comment">   * <span class="hljs-doctag">@param</span> head</span><br><span class="hljs-comment">   * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">   */</span><br>  <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> ListNode <span class="hljs-title function_">reverseListByDummyNotCreate</span><span class="hljs-params">(ListNode head)</span> &#123;<br>      <span class="hljs-type">ListNode</span> <span class="hljs-variable">ans</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ListNode</span>(-<span class="hljs-number">1</span>);<br>      <span class="hljs-type">ListNode</span> <span class="hljs-variable">cur</span> <span class="hljs-operator">=</span> head;<br>      <span class="hljs-keyword">while</span> (cur != <span class="hljs-literal">null</span>) &#123;<br>          <span class="hljs-type">ListNode</span> <span class="hljs-variable">next</span> <span class="hljs-operator">=</span> cur.next;<br>          cur.next = ans.next;<br>          ans.next = cur;<br>          cur = next;<br>      &#125;<br>      <span class="hljs-keyword">return</span> ans.next;<br>  &#125;<br></code></pre></td></tr></table></figure>

<ul>
<li><h5 id="如图所示："><a href="#如图所示：" class="headerlink" title="如图所示："></a>如图所示：</h5></li>
</ul>
<p><img src="https://gitee.com/deng-2022/pictures/raw/master/images/image-20230728093915867.png" alt="image-20230728093915867"></p>
<ul>
<li><h5 id="直接反转，具体代码如下："><a href="#直接反转，具体代码如下：" class="headerlink" title="直接反转，具体代码如下："></a>直接反转，具体代码如下：</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">    * 方法2：直接反转</span><br><span class="hljs-comment">    *</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@param</span> head</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">    */</span><br>   <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> ListNode <span class="hljs-title function_">reverseListSimple</span><span class="hljs-params">(ListNode head)</span> &#123;<br>       <span class="hljs-type">ListNode</span> <span class="hljs-variable">prev</span> <span class="hljs-operator">=</span> <span class="hljs-literal">null</span>;<br>       <span class="hljs-type">ListNode</span> <span class="hljs-variable">curr</span> <span class="hljs-operator">=</span> head;<br>       <span class="hljs-keyword">while</span> (curr != <span class="hljs-literal">null</span>) &#123;<br>           <span class="hljs-type">ListNode</span> <span class="hljs-variable">next</span> <span class="hljs-operator">=</span> curr.next;<br>           curr.next = prev;<br>           prev = curr;<br>           curr = next;<br>       &#125;<br>       <span class="hljs-keyword">return</span> prev;<br>   &#125;<br></code></pre></td></tr></table></figure>

<ul>
<li><h5 id="如图所示：（2023-07-28午）"><a href="#如图所示：（2023-07-28午）" class="headerlink" title="如图所示：（2023/07/28午）"></a>如图所示：（2023/07/28午）</h5></li>
</ul>
<p><img src="https://gitee.com/deng-2022/pictures/raw/master/images/image-20230728103908941.png" alt="image-20230728103908941"></p>
<h3 id="拓展训练（白银挑战）"><a href="#拓展训练（白银挑战）" class="headerlink" title="拓展训练（白银挑战）"></a>拓展训练（白银挑战）</h3><h4 id="区间反转链表"><a href="#区间反转链表" class="headerlink" title="区间反转链表"></a>区间反转链表</h4><ul>
<li><h5 id="给定一个区间，反转区间内的链表："><a href="#给定一个区间，反转区间内的链表：" class="headerlink" title="给定一个区间，反转区间内的链表："></a>给定一个区间，反转区间内的链表：</h5></li>
<li><h5 id="头插法："><a href="#头插法：" class="headerlink" title="头插法："></a>头插法：</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 方法1：头插法</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> head</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> left</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> right</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> ListNode <span class="hljs-title function_">reverseBetween2</span><span class="hljs-params">(ListNode head, <span class="hljs-type">int</span> left, <span class="hljs-type">int</span> right)</span> &#123;<br>        <span class="hljs-comment">// 1.定义虚拟头节点</span><br>        <span class="hljs-type">ListNode</span> <span class="hljs-variable">dummyNode</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ListNode</span>(-<span class="hljs-number">1</span>);<br>        dummyNode.next = head;<br>        <span class="hljs-type">ListNode</span> <span class="hljs-variable">pre</span> <span class="hljs-operator">=</span> dummyNode;<br>        <span class="hljs-comment">// 2.pre到达left前一节点</span><br>        <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i &lt; left - <span class="hljs-number">1</span>; i++) &#123;<br>            pre = pre.next;<br>        &#125;<br>        <span class="hljs-comment">// 3.cur指向left</span><br>        <span class="hljs-type">ListNode</span> <span class="hljs-variable">cur</span> <span class="hljs-operator">=</span> pre.next;<br>        ListNode next;<br>        <span class="hljs-comment">// 4.反转链表</span><br>        <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i &lt; right - left; i++) &#123;<br>            next = cur.next;<br>            cur.next = next.next;<br>            next.next = pre.next;<br>            pre.next = next;<br>        &#125;<br>        <span class="hljs-comment">// 5.返回头节点</span><br>        <span class="hljs-keyword">return</span> dummyNode.next;<br>    &#125;<br></code></pre></td></tr></table></figure>

<ul>
<li><h5 id="穿针引线法：（2023-07-31午）"><a href="#穿针引线法：（2023-07-31午）" class="headerlink" title="穿针引线法：（2023/07/31午）"></a>穿针引线法：（2023/07/31午）</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">    * 方法1：穿针引线法</span><br><span class="hljs-comment">    *</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@param</span> head</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@param</span> left</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@param</span> right</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">    */</span><br>   <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> ListNode <span class="hljs-title function_">reverseBetween</span><span class="hljs-params">(ListNode head, <span class="hljs-type">int</span> left, <span class="hljs-type">int</span> right)</span> &#123;<br>       <span class="hljs-comment">// 1.定义头节点</span><br>       <span class="hljs-type">ListNode</span> <span class="hljs-variable">dummyNode</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ListNode</span>(-<span class="hljs-number">1</span>);<br>       dummyNode.next = head;<br><br>       <span class="hljs-type">ListNode</span> <span class="hljs-variable">pre</span> <span class="hljs-operator">=</span> dummyNode;<br>       <span class="hljs-comment">// 2.pre来到left - 1个节点处，记录</span><br>       <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i &lt; left - <span class="hljs-number">1</span>; i++) &#123;<br>           pre = pre.next;<br>       &#125;<br><br>       <span class="hljs-comment">// 3.pre来到right节点处，记录</span><br>       <span class="hljs-type">ListNode</span> <span class="hljs-variable">rightNode</span> <span class="hljs-operator">=</span> pre;<br>       <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i &lt; right - left + <span class="hljs-number">1</span>; i++) &#123;<br>           rightNode = rightNode.next;<br>       &#125;<br><br>       <span class="hljs-comment">// 4.截断链表</span><br>       <span class="hljs-type">ListNode</span> <span class="hljs-variable">leftNode</span> <span class="hljs-operator">=</span> pre.next;<br>       <span class="hljs-type">ListNode</span> <span class="hljs-variable">succ</span> <span class="hljs-operator">=</span> rightNode.next;<br><br>       pre.next = <span class="hljs-literal">null</span>;<br>       rightNode.next = <span class="hljs-literal">null</span>;<br><br>       <span class="hljs-comment">// 5.反转链表</span><br>       reverseList(leftNode);<br><br>       <span class="hljs-comment">// 6.拼接链表</span><br>       pre.next = rightNode;<br>       leftNode.next = succ;<br>       <br>       <span class="hljs-keyword">return</span> dummyNode.next;<br>   &#125;<br></code></pre></td></tr></table></figure>

<h4 id="两两交换链表中的节点"><a href="#两两交换链表中的节点" class="headerlink" title="两两交换链表中的节点"></a>两两交换链表中的节点</h4><ul>
<li><h5 id="这是什么操作呢，如下："><a href="#这是什么操作呢，如下：" class="headerlink" title="这是什么操作呢，如下："></a>这是什么操作呢，如下：</h5></li>
</ul>
<p><img src="https://gitee.com/deng-2022/pictures/raw/master/images/image-20230802122528983.png" alt="image-20230802122528983"></p>
<ul>
<li><h5 id="这就是区间反转链表的拓展了，无非是区间长度固定为2，一次性反转多个区间"><a href="#这就是区间反转链表的拓展了，无非是区间长度固定为2，一次性反转多个区间" class="headerlink" title="这就是区间反转链表的拓展了，无非是区间长度固定为2，一次性反转多个区间"></a>这就是区间反转链表的拓展了，无非是区间长度固定为2，一次性反转多个区间</h5></li>
<li><h5 id="具体代码如下：-6"><a href="#具体代码如下：-6" class="headerlink" title="具体代码如下："></a>具体代码如下：</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 两两反转</span><br><span class="hljs-comment"> *</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@param</span> head</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment"> */</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> ListNode <span class="hljs-title function_">swapPairs</span><span class="hljs-params">(ListNode head)</span> &#123;<br>    <span class="hljs-comment">// 1.虚拟头节点</span><br>    <span class="hljs-type">ListNode</span> <span class="hljs-variable">dummyHead</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ListNode</span>(<span class="hljs-number">0</span>);<br>    dummyHead.next = head;<br>    <span class="hljs-type">ListNode</span> <span class="hljs-variable">cur</span> <span class="hljs-operator">=</span> dummyHead;<br>    <span class="hljs-keyword">while</span> (cur.next != <span class="hljs-literal">null</span> &amp;&amp; cur.next.next != <span class="hljs-literal">null</span>) &#123;<br>        <span class="hljs-comment">// 2.拿取反转两节点</span><br>        <span class="hljs-type">ListNode</span> <span class="hljs-variable">node1</span> <span class="hljs-operator">=</span> cur.next;<br>        <span class="hljs-type">ListNode</span> <span class="hljs-variable">node2</span> <span class="hljs-operator">=</span> cur.next.next;<br>        <span class="hljs-comment">// 3.两两反转</span><br>        cur.next = node2;<br>        node1.next = node2.next;<br>        node2.next = node1;<br>        cur = node1;<br>    &#125;<br>    <span class="hljs-comment">// 4.返回头节点</span><br>    <span class="hljs-keyword">return</span> dummyHead.next;<br>&#125;<br></code></pre></td></tr></table></figure>

<h4 id="链表相加"><a href="#链表相加" class="headerlink" title="链表相加"></a>链表相加</h4><ul>
<li><h5 id="链表相加，操作如下："><a href="#链表相加，操作如下：" class="headerlink" title="链表相加，操作如下："></a>链表相加，操作如下：</h5></li>
</ul>
<figure class="highlight clean"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs clean">输入：<span class="hljs-number">1</span> -&gt; <span class="hljs-number">3</span> -&gt; <span class="hljs-number">6</span>      输出：<span class="hljs-number">4</span> -&gt; <span class="hljs-number">1</span> -&gt; <span class="hljs-number">3</span><br>     <span class="hljs-number">2</span> -&gt; <span class="hljs-number">7</span> -&gt; <span class="hljs-number">7</span><br><br>即：<span class="hljs-number">136</span> + <span class="hljs-number">277</span> = <span class="hljs-number">413</span><br></code></pre></td></tr></table></figure>

<ul>
<li><h5 id="即将链表的数据域相连，当作数字进行相加操作，从低位开始相加，涉及到进位，有两种实现思路："><a href="#即将链表的数据域相连，当作数字进行相加操作，从低位开始相加，涉及到进位，有两种实现思路：" class="headerlink" title="即将链表的数据域相连，当作数字进行相加操作，从低位开始相加，涉及到进位，有两种实现思路："></a>即将链表的数据域相连，当作数字进行相加操作，从低位开始相加，涉及到进位，有两种实现思路：</h5><ul>
<li>压栈</li>
<li>反转链表</li>
</ul>
</li>
<li><h5 id="具体代码如下：-7"><a href="#具体代码如下：-7" class="headerlink" title="具体代码如下："></a>具体代码如下：</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 压栈</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> head1</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> head2</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> ListNode <span class="hljs-title function_">addInListByStack</span><span class="hljs-params">(ListNode head1, ListNode head2)</span> &#123;<br>        <span class="hljs-comment">// 1.两链表全入栈</span><br>        Stack&lt;ListNode&gt; st1 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Stack</span>&lt;ListNode&gt;();<br>        Stack&lt;ListNode&gt; st2 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Stack</span>&lt;ListNode&gt;();<br>        <span class="hljs-keyword">while</span> (head1 != <span class="hljs-literal">null</span>) &#123;<br>            st1.push(head1);<br>            head1 = head1.next;<br>        &#125;<br>        <span class="hljs-keyword">while</span> (head2 != <span class="hljs-literal">null</span>) &#123;<br>            st2.push(head2);<br>            head2 = head2.next;<br>        &#125;<br>        <br>        <span class="hljs-comment">// 2.定义进位、虚拟头节点</span><br>        <span class="hljs-type">ListNode</span> <span class="hljs-variable">dummy</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ListNode</span>(-<span class="hljs-number">1</span>);<br>        <span class="hljs-type">int</span> <span class="hljs-variable">carry</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;<br><br>        <span class="hljs-comment">//这里设置carry!=0,是因为当st1,st2都遍历完时，如果carry=0,就不需要进入循环了</span><br>        <span class="hljs-keyword">while</span> (!st1.empty() || !st2.empty() || carry != <span class="hljs-number">0</span>) &#123;<br>            <span class="hljs-comment">// 1.出栈, 相加</span><br>            <span class="hljs-type">ListNode</span> <span class="hljs-variable">a</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ListNode</span>(<span class="hljs-number">0</span>);<br>            <span class="hljs-type">ListNode</span> <span class="hljs-variable">b</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ListNode</span>(<span class="hljs-number">0</span>);<br>            <span class="hljs-keyword">if</span> (!st1.empty()) &#123;<br>                a = st1.pop();<br>            &#125;<br>            <span class="hljs-keyword">if</span> (!st2.empty()) &#123;<br>                b = st2.pop();<br>            &#125;<br>            <span class="hljs-type">int</span> <span class="hljs-variable">get_sum</span> <span class="hljs-operator">=</span> a.val + b.val + carry;<br>            <span class="hljs-comment">// 2.取余数，取进位</span><br>            <span class="hljs-type">int</span> <span class="hljs-variable">ans</span> <span class="hljs-operator">=</span> get_sum % <span class="hljs-number">10</span>;<br>            carry = get_sum / <span class="hljs-number">10</span>;<br>            <span class="hljs-comment">// 3.链表链接</span><br>            <span class="hljs-type">ListNode</span> <span class="hljs-variable">cur</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ListNode</span>(ans);<br>            cur.next = dummy.next;<br>            dummy.next = cur;<br>        &#125;<br>        <br>        <span class="hljs-comment">// 4.返回头节点</span><br>        <span class="hljs-keyword">return</span> dummy.next;<br>    &#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 方法2：链表反转</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> head1</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> head2</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> ListNode <span class="hljs-title function_">addInListByReverse</span><span class="hljs-params">(ListNode head1, ListNode head2)</span> &#123;<br>        <span class="hljs-comment">// 1.分别反转链表</span><br>        head1 = reverse(head1);<br>        head2 = reverse(head2);<br>        <span class="hljs-comment">// 2.定义进位、虚拟头节点</span><br>        <span class="hljs-type">ListNode</span> <span class="hljs-variable">head</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ListNode</span>(-<span class="hljs-number">1</span>);<br>        <span class="hljs-type">ListNode</span> <span class="hljs-variable">cur</span> <span class="hljs-operator">=</span> head;<br>        <span class="hljs-type">int</span> <span class="hljs-variable">carry</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;<br><br>        <span class="hljs-keyword">while</span> (head1 != <span class="hljs-literal">null</span> || head2 != <span class="hljs-literal">null</span>) &#123;<br>            <span class="hljs-comment">// 3.链表相加</span><br>            <span class="hljs-type">int</span> <span class="hljs-variable">val</span> <span class="hljs-operator">=</span> carry;<br>            <span class="hljs-keyword">if</span> (head1 != <span class="hljs-literal">null</span>) &#123;<br>                val += head1.val;<br>                head1 = head1.next;<br>            &#125;<br>            <span class="hljs-keyword">if</span> (head2 != <span class="hljs-literal">null</span>) &#123;<br>                val += head2.val;<br>                head2 = head2.next;<br>            &#125;<br>            <span class="hljs-comment">// 4.取进位、求余</span><br>            cur.next = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ListNode</span>(val % <span class="hljs-number">10</span>);<br>            carry = val / <span class="hljs-number">10</span>;<br>            cur = cur.next;<br>        &#125;<br>        <span class="hljs-comment">// 5.进位处理</span><br>        <span class="hljs-keyword">if</span> (carry &gt; <span class="hljs-number">0</span>) &#123;<br>            cur.next = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ListNode</span>(carry);<br>        &#125;<br>        <span class="hljs-comment">// 6.返回头节点</span><br>        <span class="hljs-keyword">return</span> reverse(head.next);<br>    &#125;<br></code></pre></td></tr></table></figure>

<h4 id="奇怪的力扣试题（链表相加）"><a href="#奇怪的力扣试题（链表相加）" class="headerlink" title="奇怪的力扣试题（链表相加）"></a>奇怪的力扣试题（链表相加）</h4><ul>
<li>妈的，今晚再提交一道题就完成任务了，我还选择了这道题，原以为会很顺利<strong>（2023/11/10晚）</strong></li>
</ul>
<blockquote>
<p>🍖 力扣原题链接：<a target="_blank" rel="noopener" href="https://leetcode.cn/problems/add-two-numbers/description/?envType=study-plan-v2&envId=top-interview-150">2. 两数相加 - 力扣（LeetCode）</a></p>
</blockquote>
<ul>
<li>逼着我认真分析了一遍构造链表的几种方法，捋了捋链表节点插入的各种场景（<strong>直接插入 / 虚拟头节点</strong>，<strong>前插 / 后插</strong>）</li>
<li>没有解决问题，最后发现题目要求是这样的：</li>
</ul>
<p><img src="https://gitee.com/deng-2022/pictures/raw/master/images/image-20231110214058204.png" alt="image-20231110214058204"></p>
<ul>
<li>它怎么是这样的？难道不是这样的吗：</li>
</ul>
<p><img src="https://gitee.com/deng-2022/pictures/raw/master/images/image-20231110214300447.png" alt="image-20231110214300447"></p>
<ul>
<li>妈的，什么破题，今晚不搞这道题了 🍟<strong>（2023/11/10晚）</strong></li>
</ul>
<h4 id="单链表加一"><a href="#单链表加一" class="headerlink" title="单链表加一"></a>单链表加一</h4><ul>
<li><h5 id="单链表加一，操作如下："><a href="#单链表加一，操作如下：" class="headerlink" title="单链表加一，操作如下："></a>单链表加一，操作如下：</h5></li>
</ul>
<figure class="highlight clean"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs clean">输入：<span class="hljs-number">1</span> -&gt; <span class="hljs-number">3</span> -&gt; <span class="hljs-number">6</span><br>输出：<span class="hljs-number">1</span> -&gt; <span class="hljs-number">3</span> -&gt; <span class="hljs-number">7</span><br></code></pre></td></tr></table></figure>

<ul>
<li><h5 id="本质上就是链表相加，只不过另外一条链表只有一个节点，从低位开始加一，涉及到进位，同样有两种实现思路："><a href="#本质上就是链表相加，只不过另外一条链表只有一个节点，从低位开始加一，涉及到进位，同样有两种实现思路：" class="headerlink" title="本质上就是链表相加，只不过另外一条链表只有一个节点，从低位开始加一，涉及到进位，同样有两种实现思路："></a>本质上就是链表相加，只不过另外一条链表只有一个节点，从低位开始加一，涉及到进位，同样有两种实现思路：</h5><ul>
<li>压栈</li>
<li>反转链表</li>
</ul>
</li>
<li><h5 id="这里仅展示压栈法，具体代码如下：（2023-07-31晚）"><a href="#这里仅展示压栈法，具体代码如下：（2023-07-31晚）" class="headerlink" title="这里仅展示压栈法，具体代码如下：（2023/07/31晚）"></a>这里仅展示压栈法，具体代码如下：（2023/07/31晚）</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 压栈</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> head</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> ListNode <span class="hljs-title function_">plusOne</span><span class="hljs-params">(ListNode head)</span> &#123;<br>        <span class="hljs-comment">// 1.全部压栈</span><br>        Stack&lt;Integer&gt; st = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Stack</span>();<br>        <span class="hljs-keyword">while</span> (head != <span class="hljs-literal">null</span>) &#123;<br>            st.push(head.val);<br>            head = head.next;<br>        &#125;<br>        <span class="hljs-comment">// 2.定义进位、加数、虚拟头节点</span><br>        <span class="hljs-type">int</span> <span class="hljs-variable">carry</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;<br>        <span class="hljs-type">int</span> <span class="hljs-variable">adder</span> <span class="hljs-operator">=</span> <span class="hljs-number">1</span>;<br>        <span class="hljs-type">ListNode</span> <span class="hljs-variable">dummy</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ListNode</span>(<span class="hljs-number">0</span>);<br><br>        <span class="hljs-keyword">while</span> (!st.empty() || adder != <span class="hljs-number">0</span> || carry &gt; <span class="hljs-number">0</span>) &#123;<br>            <span class="hljs-comment">// 4.执行加法运算</span><br>            <span class="hljs-type">int</span> <span class="hljs-variable">digit</span> <span class="hljs-operator">=</span> st.pop();<br>            <span class="hljs-type">int</span> <span class="hljs-variable">sum</span> <span class="hljs-operator">=</span> digit + adder + carry;<br>            <span class="hljs-comment">// 5.保存进位和各位结果</span><br>            carry = sum &gt;= <span class="hljs-number">10</span> ? <span class="hljs-number">1</span> : <span class="hljs-number">0</span>;<br>            sum = sum &gt;= <span class="hljs-number">10</span> ? sum - <span class="hljs-number">10</span> : sum;<br>            <span class="hljs-type">ListNode</span> <span class="hljs-variable">cur</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ListNode</span>(sum);<br>            <span class="hljs-comment">// 6.链表链接</span><br>            cur.next = dummy.next;<br>            dummy.next = cur;<br>            adder = <span class="hljs-number">0</span>;<br>        &#125;<br>        <span class="hljs-comment">// 7.返回头节点</span><br>        <span class="hljs-keyword">return</span> dummy.next;<br>    &#125;<br></code></pre></td></tr></table></figure>

<h3 id="通关（过关挑战）-1"><a href="#通关（过关挑战）-1" class="headerlink" title="通关（过关挑战）"></a>通关（过关挑战）</h3><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs"><br></code></pre></td></tr></table></figure>

<h2 id="数组"><a href="#数组" class="headerlink" title="数组"></a>数组</h2><h3 id="基础知识（青铜挑战）-1"><a href="#基础知识（青铜挑战）-1" class="headerlink" title="基础知识（青铜挑战）"></a>基础知识（青铜挑战）</h3><ul>
<li><h5 id="有关线性表的内容，这里不过多介绍，详情可看讲义（2023-08-09早）"><a href="#有关线性表的内容，这里不过多介绍，详情可看讲义（2023-08-09早）" class="headerlink" title="有关线性表的内容，这里不过多介绍，详情可看讲义（2023/08/09早）"></a>有关线性表的内容，这里不过多介绍，详情可看讲义（2023/08/09早）</h5></li>
<li><p>数组的基本操作：</p>
<ul>
<li>数组的创建和初始化</li>
<li>元素查询</li>
<li>插入元素</li>
<li>删除元素</li>
</ul>
</li>
<li><h5 id="初始化数组："><a href="#初始化数组：" class="headerlink" title="初始化数组："></a>初始化数组：</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">  * 初始化数组</span><br><span class="hljs-comment">  */</span><br> <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">initArray</span><span class="hljs-params">()</span> &#123;<br>     <span class="hljs-type">int</span>[] arr = <span class="hljs-keyword">new</span> <span class="hljs-title class_">int</span>[<span class="hljs-number">10</span>];<br>     <span class="hljs-type">int</span>[] arr2 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">int</span>[]&#123;<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>, <span class="hljs-number">9</span>, <span class="hljs-number">10</span>&#125;;<br>     <span class="hljs-type">int</span>[] arr3 = &#123;<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>, <span class="hljs-number">9</span>, <span class="hljs-number">10</span>&#125;;<br> &#125;<br></code></pre></td></tr></table></figure>

<ul>
<li><h5 id="元素查询很常见，展示一下最简单的查询方式："><a href="#元素查询很常见，展示一下最简单的查询方式：" class="headerlink" title="元素查询很常见，展示一下最简单的查询方式："></a>元素查询很常见，展示一下最简单的查询方式：</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 查询单个元素</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> arr     数组</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> size    元素数量</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> element 查询元素</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> element</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-type">int</span> <span class="hljs-title function_">findByElement</span><span class="hljs-params">(<span class="hljs-type">int</span>[] arr, <span class="hljs-type">int</span> size, <span class="hljs-type">int</span> element)</span> &#123;<br>        <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i &lt; size; i++) &#123;<br>            <span class="hljs-keyword">if</span> (arr[i] == element)<br>                <span class="hljs-keyword">return</span> element;<br>        &#125;<br>        <span class="hljs-keyword">return</span> -<span class="hljs-number">1</span>;<br>    &#125;<br></code></pre></td></tr></table></figure>

<ul>
<li><h5 id="插入元素："><a href="#插入元素：" class="headerlink" title="插入元素："></a>插入元素：</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/***</span><br><span class="hljs-comment"> * 插入数据</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@param</span> arr 数组</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@param</span> size 元素个数</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@param</span> element 插入元素</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@return</span> 下标</span><br><span class="hljs-comment"> */</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-type">int</span> <span class="hljs-title function_">addByElementSequence</span><span class="hljs-params">(<span class="hljs-type">int</span>[] arr, <span class="hljs-type">int</span> size, <span class="hljs-type">int</span> element)</span> &#123;<br>    <span class="hljs-comment">// 1.</span><br>    <span class="hljs-keyword">if</span> (size &gt;= arr.length)<br>        <span class="hljs-keyword">return</span> -<span class="hljs-number">1</span>;<br><br>    <span class="hljs-comment">// 2.默认插入元素在数组末</span><br>    <span class="hljs-type">int</span> <span class="hljs-variable">index</span> <span class="hljs-operator">=</span> size;<br>    <span class="hljs-comment">// 3.查找插入位置</span><br>    <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i &lt; size; i++) &#123;<br>        <span class="hljs-keyword">if</span> (element &lt; arr[i]) &#123;<br>            index = i;<br>            <span class="hljs-keyword">break</span>;<br>        &#125;<br>    &#125;<br>    <span class="hljs-comment">// 4.元素后移</span><br>    <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">j</span> <span class="hljs-operator">=</span> size; j &gt; index; j--) &#123;<br>        arr[j] = arr[j - <span class="hljs-number">1</span>];<br>    &#125;<br>    arr[index] = element;<br>    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<ul>
<li><h5 id="删除元素：（2023-08-09早）"><a href="#删除元素：（2023-08-09早）" class="headerlink" title="删除元素：（2023/08/09早）"></a>删除元素：（2023/08/09早）</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 删除元素</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> arr  数组</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> size 数组元素个数</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> key  删除的下标</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> size</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-type">int</span> <span class="hljs-title function_">removeByElement</span><span class="hljs-params">(<span class="hljs-type">int</span>[] arr, <span class="hljs-type">int</span> size, <span class="hljs-type">int</span> key)</span> &#123;<br>        <span class="hljs-comment">// 1.锁定删除元素下标</span><br>        <span class="hljs-type">int</span> <span class="hljs-variable">index</span> <span class="hljs-operator">=</span> -<span class="hljs-number">1</span>;<br>        <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i &lt; size; i++) &#123;<br>            <span class="hljs-keyword">if</span> (arr[i] == key) &#123;<br>                index = i;<br>                <span class="hljs-keyword">break</span>;<br>            &#125;<br>        &#125;<br><br>        <span class="hljs-keyword">if</span> (index != -<span class="hljs-number">1</span>) &#123;<br>            <span class="hljs-comment">// 2.删除元素</span><br>            <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> index + <span class="hljs-number">1</span>; i &lt; size; i++) &#123;<br>                arr[i - <span class="hljs-number">1</span>] = arr[i];<br>            &#125;<br>            <span class="hljs-comment">// 3.更新数组元素个数</span><br>            size--;<br>        &#125;<br>        <span class="hljs-keyword">return</span> size;<br>    &#125;<br></code></pre></td></tr></table></figure>

<h4 id="单调数组问题"><a href="#单调数组问题" class="headerlink" title="单调数组问题"></a>单调数组问题</h4><ul>
<li><p>数组单调有两种情况：单调递增、单调递减</p>
</li>
<li><h5 id="如何判断数组是否为单调数组呢？（2023-08-11早）"><a href="#如何判断数组是否为单调数组呢？（2023-08-11早）" class="headerlink" title="如何判断数组是否为单调数组呢？（2023/08/11早）"></a>如何判断数组是否为单调数组呢？（2023/08/11早）</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">   * 第一种方法，两次遍历确定，第一次确定是否递增 ，第二次</span><br><span class="hljs-comment">   *</span><br><span class="hljs-comment">   * <span class="hljs-doctag">@param</span> nums</span><br><span class="hljs-comment">   * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">   */</span><br>  <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-type">boolean</span> <span class="hljs-title function_">isMonotonic</span><span class="hljs-params">(<span class="hljs-type">int</span>[] nums)</span> &#123;<br>      <span class="hljs-comment">// 递增或递减</span><br>      <span class="hljs-keyword">return</span> isSorted(nums, <span class="hljs-literal">true</span>) || isSorted(nums, <span class="hljs-literal">false</span>);<br>  &#125;<br><br>  <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-type">boolean</span> <span class="hljs-title function_">isSorted</span><span class="hljs-params">(<span class="hljs-type">int</span>[] nums, <span class="hljs-type">boolean</span> increasing)</span> &#123;<br>      <span class="hljs-type">int</span> <span class="hljs-variable">n</span> <span class="hljs-operator">=</span> nums.length;<br>      <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i &lt; n - <span class="hljs-number">1</span>; ++i) &#123;<br>          <span class="hljs-comment">// 1.递增</span><br>          <span class="hljs-keyword">if</span> (increasing) &#123;<br>              <span class="hljs-keyword">if</span> (nums[i] &gt; nums[i + <span class="hljs-number">1</span>]) &#123;<br>                  <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;<br>              &#125;<br>              <span class="hljs-comment">// 2.递减</span><br>          &#125; <span class="hljs-keyword">else</span> &#123;<br>              <span class="hljs-keyword">if</span> (nums[i] &lt; nums[i + <span class="hljs-number">1</span>]) &#123;<br>                  <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;<br>              &#125;<br>          &#125;<br>      &#125;<br>      <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;<br>  &#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 第二种方式，一次遍历确定</span><br><span class="hljs-comment"> * 如果是递增的就一定不能出现递减的相邻元素，</span><br><span class="hljs-comment"> * 如果出现递减的就一定不能出现递增的相邻元素。</span><br><span class="hljs-comment"> *</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@param</span> nums</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment"> */</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-type">boolean</span> <span class="hljs-title function_">isMonotonic_2</span><span class="hljs-params">(<span class="hljs-type">int</span>[] nums)</span> &#123;<br>    <span class="hljs-type">boolean</span> <span class="hljs-variable">inc</span> <span class="hljs-operator">=</span> <span class="hljs-literal">true</span>, dec = <span class="hljs-literal">true</span>;<br>    <span class="hljs-type">int</span> <span class="hljs-variable">n</span> <span class="hljs-operator">=</span> nums.length;<br>    <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i &lt; n - <span class="hljs-number">1</span>; ++i) &#123;<br>        <span class="hljs-comment">// 1.递增</span><br>        <span class="hljs-keyword">if</span> (nums[i] &gt; nums[i + <span class="hljs-number">1</span>]) &#123;<br>            inc = <span class="hljs-literal">false</span>;<br>        &#125;<br>        <span class="hljs-comment">// 2.递减</span><br>        <span class="hljs-keyword">if</span> (nums[i] &lt; nums[i + <span class="hljs-number">1</span>]) &#123;<br>            dec = <span class="hljs-literal">false</span>;<br>        &#125;<br>    &#125;<br>    <span class="hljs-keyword">return</span> inc || dec;<br>&#125;<br></code></pre></td></tr></table></figure>

<h4 id="简单的二分查找"><a href="#简单的二分查找" class="headerlink" title="简单的二分查找"></a>简单的二分查找</h4><ul>
<li><h5 id="在单调数组中查找目标元素，最便捷的方法就是二分查找：（2023-08-11早）"><a href="#在单调数组中查找目标元素，最便捷的方法就是二分查找：（2023-08-11早）" class="headerlink" title="在单调数组中查找目标元素，最便捷的方法就是二分查找：（2023/08/11早）"></a>在单调数组中查找目标元素，最便捷的方法就是二分查找：（2023/08/11早）</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**  </span><br><span class="hljs-comment"> * 二分查找  </span><br><span class="hljs-comment"> *  </span><br><span class="hljs-comment"> * <span class="hljs-doctag">@param</span> nums 排序好的数组  </span><br><span class="hljs-comment"> * <span class="hljs-doctag">@param</span> target 目标数字  </span><br><span class="hljs-comment"> * <span class="hljs-doctag">@return</span> 如果找到目标数字，则返回其下标；否则返回-1  </span><br><span class="hljs-comment"> */</span>  <br><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-type">int</span> <span class="hljs-title function_">search</span><span class="hljs-params">(<span class="hljs-type">int</span>[] nums, <span class="hljs-type">int</span> target)</span> &#123;  <br>    <span class="hljs-type">int</span> <span class="hljs-variable">n</span> <span class="hljs-operator">=</span> nums.length;  <br>    <span class="hljs-keyword">if</span> (n == <span class="hljs-number">0</span>) &#123;  <br>        <span class="hljs-keyword">return</span> -<span class="hljs-number">1</span>; <span class="hljs-comment">// 如果数组为空，直接返回-1  </span><br>    &#125;  <br>      <br>    <span class="hljs-comment">// 左边界, 右边界  </span><br>    <span class="hljs-type">int</span> <span class="hljs-variable">left</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>, right = n - <span class="hljs-number">1</span>;   <br>      <br>    <span class="hljs-keyword">while</span> (left &lt;= right) &#123;  <br>        <span class="hljs-comment">// 中点  </span><br>        <span class="hljs-type">int</span> <span class="hljs-variable">mid</span> <span class="hljs-operator">=</span> left + (right - left) / <span class="hljs-number">2</span>; <span class="hljs-comment">// 防止left和right过大时相加导致溢出  </span><br>          <br>        <span class="hljs-keyword">if</span> (nums[mid] == target) &#123;  <br>            <span class="hljs-keyword">return</span> mid; <span class="hljs-comment">// 如果找到目标值，直接返回其下标  </span><br>        &#125; <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (nums[mid] &lt; target) &#123;  <br>            left = mid + <span class="hljs-number">1</span>; <span class="hljs-comment">// 如果目标值大于mid对应的值，则在右半部分继续查找  </span><br>        &#125; <span class="hljs-keyword">else</span> &#123;  <br>            right = mid - <span class="hljs-number">1</span>; <span class="hljs-comment">// 如果目标值小于mid对应的值，则在左半部分继续查找  </span><br>        &#125;  <br>    &#125;  <br>      <br>    <span class="hljs-keyword">return</span> -<span class="hljs-number">1</span>; <span class="hljs-comment">// 没有找到目标值，返回-1  </span><br>&#125;<br></code></pre></td></tr></table></figure>

<h4 id="数组合并"><a href="#数组合并" class="headerlink" title="数组合并"></a>数组合并</h4><ul>
<li><p>我们讨论单调数组的合并：两单调数组合并之后，结果仍然是单调数组</p>
</li>
<li><h5 id="这样的数组合并有很多种方法："><a href="#这样的数组合并有很多种方法：" class="headerlink" title="这样的数组合并有很多种方法："></a>这样的数组合并有很多种方法：</h5><ul>
<li>新数组：创建一个新数组，依次比较两数组元素大小，存放</li>
<li>不新建数组</li>
</ul>
</li>
<li><h5 id="我们仅展示第二种做法："><a href="#我们仅展示第二种做法：" class="headerlink" title="我们仅展示第二种做法："></a>我们仅展示第二种做法：</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 方法1：先合并再排序实现排序</span><br><span class="hljs-comment"> *</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@param</span> nums1 第一个数组</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@param</span> nums1_len 第一个数组的长度</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@param</span> nums2  第二个数组，将nums2合并到nums1中</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@param</span> nums2_len 第二个数组的长度</span><br><span class="hljs-comment"> */</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">merge1</span><span class="hljs-params">(<span class="hljs-type">int</span>[] nums1, <span class="hljs-type">int</span> nums1_len, <span class="hljs-type">int</span>[] nums2, <span class="hljs-type">int</span> nums2_len)</span> &#123;<br>    <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i &lt; nums2_len; ++i) &#123;<br>        nums1[nums1_len + i] = nums2[i];<br>    &#125;<br>    Arrays.sort(nums1);<br>&#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">    * 方法2：两个数组从后向前逐步合并</span><br><span class="hljs-comment">    *</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@param</span> nums1</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@param</span> nums1_len</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@param</span> nums2</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@param</span> nums2_len</span><br><span class="hljs-comment">    */</span><br>   <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">merge2</span><span class="hljs-params">(<span class="hljs-type">int</span>[] nums1, <span class="hljs-type">int</span> nums1_len, <span class="hljs-type">int</span>[] nums2, <span class="hljs-type">int</span> nums2_len)</span> &#123;<br>       <span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> nums1_len + nums2_len - <span class="hljs-number">1</span>;<br>       <span class="hljs-type">int</span> <span class="hljs-variable">len1</span> <span class="hljs-operator">=</span> nums1_len - <span class="hljs-number">1</span>, len2 = nums2_len - <span class="hljs-number">1</span>;<br>       <br>       <span class="hljs-keyword">while</span> (len1 &gt;= <span class="hljs-number">0</span> &amp;&amp; len2 &gt;= <span class="hljs-number">0</span>) &#123;<br>           <span class="hljs-keyword">if</span> (nums1[len1] &lt;= nums2[len2])<br>               nums1[i--] = nums2[len2--];<br>           <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (nums1[len1] &gt; nums2[len2])<br>               nums1[i--] = nums1[len1--];<br>       &#125;<br>       <span class="hljs-comment">//假如A或者B数组还有剩余</span><br>       <span class="hljs-keyword">while</span> (len2 != -<span class="hljs-number">1</span>) nums1[i--] = nums2[len2--];<br>       <span class="hljs-keyword">while</span> (len1 != -<span class="hljs-number">1</span>) nums1[i--] = nums1[len1--];<br>   &#125;<br></code></pre></td></tr></table></figure>

<h3 id="实战训练（白银挑战）-1"><a href="#实战训练（白银挑战）-1" class="headerlink" title="实战训练（白银挑战）"></a>实战训练（白银挑战）</h3><h4 id="双指针-1"><a href="#双指针-1" class="headerlink" title="双指针"></a>双指针</h4><ul>
<li>我们使用双指针，能够很清晰、方便地解决问题，接下来我们使用双指针来解决一些典型问题：</li>
</ul>
<h4 id="删除重复元素"><a href="#删除重复元素" class="headerlink" title="删除重复元素"></a>删除重复元素</h4><ul>
<li><h5 id="给定一个数组，要求删除其中所有重复元素，重复元素保留一个"><a href="#给定一个数组，要求删除其中所有重复元素，重复元素保留一个" class="headerlink" title="给定一个数组，要求删除其中所有重复元素，重复元素保留一个"></a>给定一个数组，要求删除其中所有重复元素，重复元素保留一个</h5></li>
<li><p>对于这种问题，我们使用快慢双指针来解决：</p>
<ul>
<li><p>快指针在前，慢指针在后，快指针向前移动</p>
</li>
<li><p>如果快指针所指元素，与此时慢指针所指元素<strong>相同</strong>（<strong>即重复元素</strong>），则慢指针等待，快指针继续向前移动</p>
</li>
<li><p>如果快指针所指元素，与此时慢指针所指元素<strong>不相同</strong>，则慢指针：先记录快指针指向的值，然后向前移动，快指针继续向前移动</p>
</li>
<li><p>循环执行，直到快指针移动到右边界为止</p>
</li>
<li><h5 id="即：慢指针左侧维护了所有未重复的元素值"><a href="#即：慢指针左侧维护了所有未重复的元素值" class="headerlink" title="即：慢指针左侧维护了所有未重复的元素值"></a>即：慢指针左侧维护了所有未重复的元素值</h5></li>
</ul>
</li>
<li><h5 id="具体代码实现如下：（2023-08-13午）"><a href="#具体代码实现如下：（2023-08-13午）" class="headerlink" title="具体代码实现如下：（2023/08/13午）"></a>具体代码实现如下：（2023/08/13午）</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">    * 快慢指针</span><br><span class="hljs-comment">    *</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@param</span> nums</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">    */</span><br>   <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-type">int</span> <span class="hljs-title function_">removeDuplicates</span><span class="hljs-params">(<span class="hljs-type">int</span>[] nums)</span> &#123;<br>       <span class="hljs-comment">// slow维护左侧，未重复元素</span><br>       <span class="hljs-type">int</span> <span class="hljs-variable">slow</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;<br>       <span class="hljs-comment">// 快指针移动</span><br>       <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">fast</span> <span class="hljs-operator">=</span> <span class="hljs-number">1</span>; fast &lt; nums.length; fast++) &#123;<br>            <span class="hljs-keyword">if</span>(nums[fast] != nums[slow])&#123;<br>               <span class="hljs-comment">// slow负责维护未重复的元素</span><br>               slow++;<br>               nums[slow] = nums[fast];<br>           &#125;<br>       &#125;<br>            <br>       <span class="hljs-keyword">return</span> slow + <span class="hljs-number">1</span>;<br>   &#125;<br></code></pre></td></tr></table></figure>

<h4 id="删除指定元素"><a href="#删除指定元素" class="headerlink" title="删除指定元素"></a>删除指定元素</h4><ul>
<li><h5 id="给定一个数组，要求删除指定元素"><a href="#给定一个数组，要求删除指定元素" class="headerlink" title="给定一个数组，要求删除指定元素"></a>给定一个数组，要求删除指定元素</h5></li>
<li><p>对于这种问题，我们也可以使用双指针解决，且有两种思路：</p>
<ul>
<li>快慢双指针</li>
<li>对撞双指针</li>
</ul>
</li>
<li><p>快慢双指针的思路与上面如出一辙，无非是：快指针寻找的元素不再是与慢指针重复的元素，而是指定元素</p>
</li>
<li><h5 id="具体代码如下：（2023-08-13午）"><a href="#具体代码如下：（2023-08-13午）" class="headerlink" title="具体代码如下：（2023/08/13午）"></a>具体代码如下：（2023/08/13午）</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 快慢双指针</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> nums</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> val</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-type">int</span> <span class="hljs-title function_">removeElement</span><span class="hljs-params">(<span class="hljs-type">int</span>[] nums, <span class="hljs-type">int</span> val)</span> &#123;<br>        <span class="hljs-type">int</span> <span class="hljs-variable">slow</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;<br>        <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">fast</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; fast &lt; nums.length; fast++) &#123;<br>            <span class="hljs-keyword">if</span> (nums[fast] != val) &#123;<br>                nums[slow] = nums[fast];<br>                slow++;<br>            &#125;<br>        &#125;<br>        <span class="hljs-comment">//最后剩余元素的数量</span><br>        <span class="hljs-keyword">return</span> slow;<br>    &#125;<br></code></pre></td></tr></table></figure>

<ul>
<li><h5 id="这里简单捋一捋对撞双指针的思路：（2023-08-13午）"><a href="#这里简单捋一捋对撞双指针的思路：（2023-08-13午）" class="headerlink" title="这里简单捋一捋对撞双指针的思路：（2023/08/13午）"></a>这里简单捋一捋对撞双指针的思路：（2023/08/13午）</h5><ul>
<li>左指针在左，右指针在右，左指针向前移动</li>
<li>如果左指针未找到指定元素，则左指针继续向前移动</li>
<li>如果左指针找到了指定元素，就拿右指针所指元素覆盖，左指针向前移动，右指针向后移动</li>
<li>循环执行，直到两指针碰撞</li>
<li>即：左指针左侧维护了所有正确的元素值</li>
</ul>
</li>
<li><h5 id="使用对撞双指针解决思路：（2023-08-15晚）"><a href="#使用对撞双指针解决思路：（2023-08-15晚）" class="headerlink" title="使用对撞双指针解决思路：（2023/08/15晚）"></a>使用对撞双指针解决思路：（2023/08/15晚）</h5><ul>
<li><p>设左右双指针</p>
</li>
<li><p>左指针向前移动，遇到指定元素就停下来</p>
</li>
<li><p>这时，将两指针所指元素交换，右指针向前一步</p>
</li>
<li><p>重复以上操作，直至两指针相撞</p>
</li>
</ul>
</li>
<li><h5 id="具体代码实现如下：（2023-08-13午）-1"><a href="#具体代码实现如下：（2023-08-13午）-1" class="headerlink" title="具体代码实现如下：（2023/08/13午）"></a>具体代码实现如下：（2023/08/13午）</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">   * 对撞双指针</span><br><span class="hljs-comment">   *</span><br><span class="hljs-comment">   * <span class="hljs-doctag">@param</span> nums</span><br><span class="hljs-comment">   * <span class="hljs-doctag">@param</span> val</span><br><span class="hljs-comment">   * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">   */</span><br>  <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-type">int</span> <span class="hljs-title function_">removeElement3</span><span class="hljs-params">(<span class="hljs-type">int</span>[] nums, <span class="hljs-type">int</span> val)</span> &#123;<br>      <span class="hljs-comment">// 1.定义左右指针</span><br>      <span class="hljs-type">int</span> <span class="hljs-variable">right</span> <span class="hljs-operator">=</span> nums.length - <span class="hljs-number">1</span>;<br>      <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">left</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; left &lt; right; ) &#123;<br>          <span class="hljs-comment">// 2.左指针找到目标元素</span><br>          <span class="hljs-keyword">if</span> (nums[left] == val) &#123;<br>              <span class="hljs-comment">// 3.右指针覆盖左指针</span><br>              nums[left] = nums[right];<br>              right--;<br>          &#125; <br>          <br>          left++;    <br>      &#125;<br>      <span class="hljs-keyword">return</span> right;<br>  &#125;<br></code></pre></td></tr></table></figure>

<h4 id="元素奇偶移动专题"><a href="#元素奇偶移动专题" class="headerlink" title="元素奇偶移动专题"></a>元素奇偶移动专题</h4><ul>
<li><p>情景：</p>
</li>
<li><h5 id="给你一个数组，设计一种算法，要求操作结束后，数组中所有偶数都在左，所有奇数都在右"><a href="#给你一个数组，设计一种算法，要求操作结束后，数组中所有偶数都在左，所有奇数都在右" class="headerlink" title="给你一个数组，设计一种算法，要求操作结束后，数组中所有偶数都在左，所有奇数都在右"></a>给你一个数组，设计一种算法，要求操作结束后，数组中所有偶数都在左，所有奇数都在右</h5></li>
<li><p>首先想到的方法应该是：new一个新数组，把该数组遍历两遍，依次将偶数元素、奇数元素先后插入 ✔</p>
</li>
<li><h5 id="那么有没有不开辟新空间，即空间复杂度为O-1-的算法呢？"><a href="#那么有没有不开辟新空间，即空间复杂度为O-1-的算法呢？" class="headerlink" title="那么有没有不开辟新空间，即空间复杂度为O(1)的算法呢？"></a>那么有没有不开辟新空间，即空间复杂度为O(1)的算法呢？</h5></li>
<li><p>当然有：</p>
<ul>
<li>设左右双指针</li>
<li>左指针遇到偶数元素就跳过，遇到奇数元素就停下来</li>
<li>右指针相反遇到奇数元素就跳过，遇到偶数元素就停下来</li>
<li>这时，将两指针指向元素交换</li>
<li>重复以上操作，直至两指针相撞</li>
</ul>
</li>
<li><h5 id="这不就是-“对撞双指针”-思想吗？，具体代码如下：（2023-08-15晚）"><a href="#这不就是-“对撞双指针”-思想吗？，具体代码如下：（2023-08-15晚）" class="headerlink" title="这不就是 “对撞双指针” 思想吗？，具体代码如下：（2023/08/15晚）"></a>这不就是 “对撞双指针” 思想吗？，具体代码如下：（2023/08/15晚）</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">  * 双指针，不稳定 的移动</span><br><span class="hljs-comment">  *</span><br><span class="hljs-comment">  * <span class="hljs-doctag">@param</span> A</span><br><span class="hljs-comment">  * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">  */</span><br> <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-type">int</span>[] sortArrayByParity(<span class="hljs-type">int</span>[] A) &#123;<br>     <span class="hljs-type">int</span> <span class="hljs-variable">left</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>, right = A.length - <span class="hljs-number">1</span>;<br>     <span class="hljs-keyword">while</span> (left &lt; right) &#123;<br>         <span class="hljs-keyword">if</span> (A[left] % <span class="hljs-number">2</span> &gt; A[right] % <span class="hljs-number">2</span>) &#123;<br>             <span class="hljs-type">int</span> <span class="hljs-variable">tmp</span> <span class="hljs-operator">=</span> A[left];<br>             A[left] = A[right];<br>             A[right] = tmp;<br>         &#125;<br><br>         <span class="hljs-keyword">if</span> (A[left] % <span class="hljs-number">2</span> == <span class="hljs-number">0</span>) left++;<br>         <span class="hljs-keyword">if</span> (A[right] % <span class="hljs-number">2</span> == <span class="hljs-number">1</span>) right--;<br>     &#125;<br><br>     <span class="hljs-keyword">return</span> A;<br> &#125;<br></code></pre></td></tr></table></figure>

<h4 id="数组轮转"><a href="#数组轮转" class="headerlink" title="数组轮转"></a>数组轮转</h4><ul>
<li><p>情景：</p>
</li>
<li><h5 id="给你一个数组，将数组的每个元素，向右轮转k个位置，得到新数组，即："><a href="#给你一个数组，将数组的每个元素，向右轮转k个位置，得到新数组，即：" class="headerlink" title="给你一个数组，将数组的每个元素，向右轮转k个位置，得到新数组，即："></a>给你一个数组，将数组的每个元素，向右轮转k个位置，得到新数组，即：</h5></li>
</ul>
<figure class="highlight accesslog"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs accesslog">输入: nums = <span class="hljs-string">[1,2,3,4,5,6,7]</span>, k = <span class="hljs-number">3</span><br>输出: <span class="hljs-string">[5,6,7,1,2,3,4]</span><br></code></pre></td></tr></table></figure>

<ul>
<li><h5 id="看起来好像挺简单，但怎么实现没思路，这里介绍一种简单的方法：两轮反转（2023-08-25早）"><a href="#看起来好像挺简单，但怎么实现没思路，这里介绍一种简单的方法：两轮反转（2023-08-25早）" class="headerlink" title="看起来好像挺简单，但怎么实现没思路，这里介绍一种简单的方法：两轮反转（2023/08/25早）"></a>看起来好像挺简单，但怎么实现没思路，这里介绍一种简单的方法：两轮反转（2023/08/25早）</h5><ul>
<li>首先，对整个数组实行翻转</li>
<li>然后，从k处将数组分隔，分别翻转分隔后的两个数组</li>
<li>这样就得到了最终结果</li>
</ul>
</li>
<li><p>具体代码如下：</p>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 数组轮转</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> nums</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> k</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">rotate</span><span class="hljs-params">(<span class="hljs-type">int</span>[] nums, <span class="hljs-type">int</span> k)</span> &#123;<br>        k %= nums.length;<br>        reverse(nums, <span class="hljs-number">0</span>, nums.length - <span class="hljs-number">1</span>);<br>        reverse(nums, <span class="hljs-number">0</span>, k - <span class="hljs-number">1</span>);<br>        reverse(nums, k, nums.length - <span class="hljs-number">1</span>);<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">reverse</span><span class="hljs-params">(<span class="hljs-type">int</span>[] nums, <span class="hljs-type">int</span> start, <span class="hljs-type">int</span> end)</span> &#123;<br>        <span class="hljs-keyword">while</span> (start &lt; end) &#123;<br>            <span class="hljs-type">int</span> <span class="hljs-variable">temp</span> <span class="hljs-operator">=</span> nums[start];<br>            nums[start] = nums[end];<br>            nums[end] = temp;<br>            start += <span class="hljs-number">1</span>;<br>            end -= <span class="hljs-number">1</span>;<br>        &#125;<br>    &#125;<br></code></pre></td></tr></table></figure>

<h4 id="数组的区间专题"><a href="#数组的区间专题" class="headerlink" title="数组的区间专题"></a>数组的区间专题</h4><ul>
<li>情景：</li>
</ul>
<figure class="highlight prolog"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs prolog">示例:<br>输入: nums = [<span class="hljs-number">0</span>,<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>,<span class="hljs-number">7</span>]<br>输出: [<span class="hljs-string">&quot;0-&gt;2&quot;</span>,<span class="hljs-string">&quot;4-&gt;5&quot;</span>,<span class="hljs-string">&quot;7&quot;</span>]<br></code></pre></td></tr></table></figure>

<ul>
<li>实现思路：<ul>
<li>定义快慢指针</li>
<li>快指针向前移动</li>
<li>快指针找到了不连续递增的数，慢指针指向该数，快指针继续移动</li>
<li>快指针到达数组边缘，结束</li>
</ul>
</li>
<li>具体代码如下：</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 区间合并</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> nums</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> List&lt;String&gt; <span class="hljs-title function_">summaryRanges</span><span class="hljs-params">(<span class="hljs-type">int</span>[] nums)</span> &#123;<br>        List&lt;String&gt; res = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayList</span>&lt;&gt;();<br>        <span class="hljs-comment">// slow 初始指向第 1 个区间的起始位置</span><br>        <span class="hljs-type">int</span> <span class="hljs-variable">slow</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;<br>        <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">fast</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; fast &lt; nums.length; fast++) &#123;<br>            <span class="hljs-comment">// fast 向后遍历，直到不满足连续递增(即 nums[fast] + 1 != nums[fast + 1])</span><br>            <span class="hljs-comment">// 或者 fast 达到数组边界，则当前连续递增区间 [slow, fast] 遍历完毕，将其写入结果列表。</span><br>            <span class="hljs-keyword">if</span> (fast + <span class="hljs-number">1</span> == nums.length || nums[fast] + <span class="hljs-number">1</span> != nums[fast + <span class="hljs-number">1</span>]) &#123;<br>                <span class="hljs-comment">// 将当前区间 [slow, fast] 写入结果列表</span><br>                <span class="hljs-type">StringBuilder</span> <span class="hljs-variable">sb</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">StringBuilder</span>();<br>                sb.append(nums[slow]);<br>                <span class="hljs-keyword">if</span> (slow != fast) &#123;<br>                    sb.append(<span class="hljs-string">&quot;-&gt;&quot;</span>).append(nums[fast]);<br>                &#125;<br>                res.add(sb.toString());<br>                <span class="hljs-comment">// 将 slow 指向更新为 fast + 1，作为下一个区间的起始位置</span><br>                slow = fast + <span class="hljs-number">1</span>;<br>            &#125;<br>        &#125;<br>        <span class="hljs-keyword">return</span> res;<br>    &#125;<br></code></pre></td></tr></table></figure>

<h4 id="字符串替换空格问题"><a href="#字符串替换空格问题" class="headerlink" title="字符串替换空格问题"></a>字符串替换空格问题</h4><ul>
<li><p>情景：</p>
</li>
<li><p>给你一个字符串，实现：将该字符串中的所有空格都替换为”20%”</p>
</li>
<li><p>这个简单，很快就能有思路：</p>
<ul>
<li><p>创建一个新的String对象newString</p>
</li>
<li><p>依次拿到字符串中的字符，检查是否为空格，拼接到newString中</p>
</li>
<li><p>如果是空格，则拼接”20%”，反之拼接该字符</p>
</li>
</ul>
</li>
<li><h5 id="具体代码如下：（2023-08-25早）"><a href="#具体代码如下：（2023-08-25早）" class="headerlink" title="具体代码如下：（2023/08/25早）"></a>具体代码如下：（2023/08/25早）</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 方法1：创建新的字符串</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> str</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> String <span class="hljs-title function_">replaceSpace1</span><span class="hljs-params">(StringBuffer str)</span> &#123;<br>        <span class="hljs-type">String</span> <span class="hljs-variable">res</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;&quot;</span>;<br>        <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i &lt; str.length(); i++) &#123;<br>            <span class="hljs-type">char</span> <span class="hljs-variable">c</span> <span class="hljs-operator">=</span> str.charAt(i);<br>            <span class="hljs-keyword">if</span> (c == <span class="hljs-string">&#x27; &#x27;</span>)<br>                res += <span class="hljs-string">&quot;%20&quot;</span>;<br>            <span class="hljs-keyword">else</span><br>                res += c;<br>        &#125;<br>        <span class="hljs-keyword">return</span> res;<br>    &#125;<br></code></pre></td></tr></table></figure>

<ul>
<li>我们创建了一个新的字符串来完成👆，那么如果我们在原字符串上，扩充所需空间，不必开辟新的空间就能完成呢？</li>
<li>思路如下：<ul>
<li>扩充字符串长度</li>
<li>定义快慢指针，快指针指向原长度末，慢指针指向现长度末</li>
<li>快指针向前移动，检查每个字符</li>
<li>如果该字符是为非空格字符，则慢指针维护该字符，两指针向前移动</li>
<li>如果是空格，慢指针维护”20%”，两指针向前移动</li>
<li>快指针到达数组边缘，结束</li>
</ul>
</li>
</ul>
<p><img src="https://gitee.com/deng-2022/pictures/raw/master/images/image-20230825115450550.png" alt="image-20230825115450550"></p>
<ul>
<li><h5 id="具体代码如下：（2023-08-25早）-1"><a href="#具体代码如下：（2023-08-25早）-1" class="headerlink" title="具体代码如下：（2023/08/25早）"></a>具体代码如下：（2023/08/25早）</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 方法2：在原数组基础上改</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> str</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> String <span class="hljs-title function_">replaceSpace2</span><span class="hljs-params">(StringBuffer str)</span> &#123;<br>        <span class="hljs-keyword">if</span> (str == <span class="hljs-literal">null</span>)<br>            <span class="hljs-keyword">return</span> <span class="hljs-literal">null</span>;<br>        <span class="hljs-type">int</span> <span class="hljs-variable">numOfblank</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;<span class="hljs-comment">//空格数量</span><br>        <span class="hljs-type">int</span> <span class="hljs-variable">len</span> <span class="hljs-operator">=</span> str.length();<br>        <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i &lt; len; i++) &#123;  <span class="hljs-comment">//计算空格数量</span><br>            <span class="hljs-keyword">if</span> (str.charAt(i) == <span class="hljs-string">&#x27; &#x27;</span>)<br>                numOfblank++;<br>        &#125;<br>        str.setLength(len + <span class="hljs-number">2</span> * numOfblank); <span class="hljs-comment">//设置长度</span><br>        <span class="hljs-type">int</span> <span class="hljs-variable">fast</span> <span class="hljs-operator">=</span> len - <span class="hljs-number">1</span>;  <span class="hljs-comment">//两个指针</span><br>        <span class="hljs-type">int</span> <span class="hljs-variable">slow</span> <span class="hljs-operator">=</span> (len + <span class="hljs-number">2</span> * numOfblank) - <span class="hljs-number">1</span>;<br><br>        <span class="hljs-keyword">while</span> (fast &gt;= <span class="hljs-number">0</span> &amp;&amp; slow &gt; fast) &#123;<br>            <span class="hljs-type">char</span> <span class="hljs-variable">c</span> <span class="hljs-operator">=</span> str.charAt(fast);<br>            <span class="hljs-keyword">if</span> (c == <span class="hljs-string">&#x27; &#x27;</span>) &#123;<br>                fast--;<br>                str.setCharAt(slow--, <span class="hljs-string">&#x27;0&#x27;</span>);<br>                str.setCharAt(slow--, <span class="hljs-string">&#x27;2&#x27;</span>);<br>                str.setCharAt(slow--, <span class="hljs-string">&#x27;%&#x27;</span>);<br>            &#125; <span class="hljs-keyword">else</span> &#123;<br>                str.setCharAt(slow, c);<br>                fast--;<br>                slow--;<br>            &#125;<br>        &#125;<br>        <span class="hljs-keyword">return</span> str.toString();<br>    &#125;<br></code></pre></td></tr></table></figure>

<h2 id="栈"><a href="#栈" class="headerlink" title="栈"></a>栈</h2><h3 id="基础知识（青铜挑战）-2"><a href="#基础知识（青铜挑战）-2" class="headerlink" title="基础知识（青铜挑战）"></a>基础知识（青铜挑战）</h3><ul>
<li><p>认识栈这个数据结构：先进先出</p>
</li>
<li><p>常用的栈操作有：</p>
<ul>
<li>push(E e)：入栈</li>
<li>peek()：弹出栈顶元素</li>
<li>pop()：出栈</li>
<li>empty()：判断栈是否为空</li>
</ul>
</li>
<li><h5 id="我们尝试手动创建一个栈数据结构，并实现它的基础功能："><a href="#我们尝试手动创建一个栈数据结构，并实现它的基础功能：" class="headerlink" title="我们尝试手动创建一个栈数据结构，并实现它的基础功能："></a>我们尝试手动创建一个栈数据结构，并实现它的基础功能：</h5></li>
</ul>
<h4 id="基于数组实现栈"><a href="#基于数组实现栈" class="headerlink" title="基于数组实现栈"></a>基于数组实现栈</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 基于数组实现</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@param</span> &lt;T&gt;</span><br><span class="hljs-comment"> */</span><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">Mystack2</span>&lt;T&gt; &#123;<br>    <span class="hljs-comment">// 内部数组</span><br>    <span class="hljs-keyword">private</span> Object[] stack;<br>    <span class="hljs-comment">// 栈顶</span><br>    <span class="hljs-keyword">private</span> <span class="hljs-type">int</span> top;<br><br>    <span class="hljs-comment">// 初始化</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-title function_">Mystack2</span><span class="hljs-params">(<span class="hljs-type">int</span> size)</span> &#123;<br>        stack = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Object</span>[size];<br>    &#125;<br><br>    <span class="hljs-comment">// 入栈</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">push</span><span class="hljs-params">(T t)</span> &#123;<br>        <span class="hljs-comment">// todo 检查数组是否已满，扩容</span><br>        <span class="hljs-built_in">this</span>.expandCapacity(top + <span class="hljs-number">1</span>);<br>        <span class="hljs-built_in">this</span>.stack[top] = t;<br>        top++;<br>    &#125;<br><br>    <span class="hljs-comment">// 弹出栈顶元素</span><br>    <span class="hljs-keyword">public</span> T <span class="hljs-title function_">peek</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-type">T</span> <span class="hljs-variable">t</span> <span class="hljs-operator">=</span> <span class="hljs-literal">null</span>;<br>        <span class="hljs-keyword">if</span> (!<span class="hljs-built_in">this</span>.empty())<br>            t = (T) <span class="hljs-built_in">this</span>.stack[top - <span class="hljs-number">1</span>];<br>        <span class="hljs-keyword">return</span> t;<br>    &#125;<br><br>    <span class="hljs-comment">// 出栈</span><br>    <span class="hljs-keyword">public</span> T <span class="hljs-title function_">pop</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-type">T</span> <span class="hljs-variable">t</span> <span class="hljs-operator">=</span> <span class="hljs-built_in">this</span>.peek();<br><br>        <span class="hljs-built_in">this</span>.stack[top - <span class="hljs-number">1</span>] = <span class="hljs-literal">null</span>;<br>        top--;<br><br>        <span class="hljs-keyword">return</span> t;<br>    &#125;<br><br>    <span class="hljs-comment">// 判空</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-type">boolean</span> <span class="hljs-title function_">empty</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>.top &lt;= <span class="hljs-number">0</span>;<br>    &#125;<br><br>    <span class="hljs-comment">// 校验数组大小，判断是否需要扩容</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">expandCapacity</span><span class="hljs-params">(<span class="hljs-type">int</span> size)</span> &#123;<br>        <span class="hljs-type">int</span> <span class="hljs-variable">capacity</span> <span class="hljs-operator">=</span> <span class="hljs-built_in">this</span>.stack.length;<br>        <span class="hljs-keyword">if</span> (size &gt; capacity) &#123;<br>            <span class="hljs-comment">// 扩容1.5倍</span><br>            capacity = capacity * <span class="hljs-number">3</span> / <span class="hljs-number">2</span> + <span class="hljs-number">1</span>;<br>            <span class="hljs-comment">// capacity = capacity + capacity &gt;&gt; 1;</span><br>            <span class="hljs-built_in">this</span>.stack = Arrays.copyOf(stack, capacity);<br>        &#125;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> &#123;<br>        Mystack2&lt;String&gt; stack = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Mystack2</span>&lt;&gt;(<span class="hljs-number">10</span>);<br>        System.out.println(stack.peek());<br>        System.out.println(stack.empty());<br>        stack.push(<span class="hljs-string">&quot;java&quot;</span>);<br>        stack.push(<span class="hljs-string">&quot;is&quot;</span>);<br>        stack.push(<span class="hljs-string">&quot;beautiful&quot;</span>);<br>        stack.push(<span class="hljs-string">&quot;language&quot;</span>);<br>        System.out.println(stack.pop());<br>        System.out.println(stack.empty());<br>        System.out.println(stack.peek());<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h4 id="基于链表实现栈"><a href="#基于链表实现栈" class="headerlink" title="基于链表实现栈"></a>基于链表实现栈</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 基于链表实现</span><br><span class="hljs-comment"> *</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@param</span> &lt;T&gt;</span><br><span class="hljs-comment"> */</span><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">ListStack2</span>&lt;T&gt; &#123;<br>    <span class="hljs-comment">// 栈顶节点</span><br>    <span class="hljs-keyword">private</span> Node head;<br><br>    <span class="hljs-comment">// 入栈</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">push</span><span class="hljs-params">(T t)</span> &#123;<br>        <span class="hljs-keyword">if</span> (t == <span class="hljs-literal">null</span>) &#123;<br>            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">NullPointerException</span>(<span class="hljs-string">&quot;参数不能为空&quot;</span>);<br>        &#125;<br><br>        <span class="hljs-keyword">if</span> (head == <span class="hljs-literal">null</span>) &#123;<br>            head = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Node</span>(t);<br>        &#125; <span class="hljs-keyword">else</span> &#123;<br>            <span class="hljs-type">Node</span> <span class="hljs-variable">node</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Node</span>(t);<br>            node.next = head;<br>            head = newNode;<br>        &#125;<br>    &#125;<br><br>    <span class="hljs-comment">// 弹出栈顶元素</span><br>    <span class="hljs-keyword">public</span> T <span class="hljs-title function_">peek</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> head.t;<br>    &#125;<br><br>    <span class="hljs-comment">// 出栈</span><br>    <span class="hljs-keyword">public</span> T <span class="hljs-title function_">pop</span><span class="hljs-params">()</span> &#123;<br>        head = head.next;<br>        <span class="hljs-keyword">return</span> head.t;<br>    &#125;<br><br>    <span class="hljs-comment">// 判断栈是否为空</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-type">boolean</span> <span class="hljs-title function_">empty</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> head == <span class="hljs-literal">null</span>;<br>    &#125;<br><br>    <span class="hljs-comment">// 节点结构</span><br>    <span class="hljs-keyword">class</span> <span class="hljs-title class_">Node</span> &#123;<br>        <span class="hljs-keyword">private</span> T t;<br>        <span class="hljs-keyword">private</span> Node next;<br><br>        <span class="hljs-keyword">public</span> <span class="hljs-title function_">Node</span><span class="hljs-params">(T t)</span> &#123;<br>            <span class="hljs-built_in">this</span>.t = t;<br>        &#125;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h3 id="实战训练（白银挑战）-2"><a href="#实战训练（白银挑战）-2" class="headerlink" title="实战训练（白银挑战）"></a>实战训练（白银挑战）</h3><h4 id="括号匹配问题"><a href="#括号匹配问题" class="headerlink" title="括号匹配问题"></a>括号匹配问题</h4><ul>
<li><p>情景：</p>
</li>
<li><h5 id="给定一个字符串，仅包含”-“、”-”、”-“、”-”等字符，要求如下："><a href="#给定一个字符串，仅包含”-“、”-”、”-“、”-”等字符，要求如下：" class="headerlink" title="给定一个字符串，仅包含”(“、”)”、”[“、”]”等字符，要求如下："></a>给定一个字符串，仅包含”(“、”)”、”[“、”]”等字符，要求如下：</h5></li>
<li><h5 id="如果该字符中的左括号与右括号连续且成对出现，则说明该字符串有效，如下："><a href="#如果该字符中的左括号与右括号连续且成对出现，则说明该字符串有效，如下：" class="headerlink" title="如果该字符中的左括号与右括号连续且成对出现，则说明该字符串有效，如下："></a>如果该字符中的左括号与右括号连续且成对出现，则说明该字符串有效，如下：</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-type">String</span> <span class="hljs-variable">str</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;()&#123;&#125;[]&quot;</span> <span class="hljs-comment">// 有效字符串</span><br></code></pre></td></tr></table></figure>

<ul>
<li><p>实现一个算法，判断该字符串是否有效</p>
</li>
<li><p>实现思路：</p>
<ul>
<li>构造一个HashMap，将左右括号以键值对形式存放进去</li>
<li>构造一个栈Stack</li>
<li>依次遍历字符串中的字符s，如果存在Map中的key值与s相等，则入栈</li>
<li>如果该字符s不是key值，即不是左括号，则Stack出栈，计算value值是否与s相等</li>
<li>不相等则说明括号<strong>不连续或不成对</strong>，该字符串是无效的</li>
</ul>
</li>
<li><h5 id="这里利用的是栈的特性：先进先出，用来判断括号是否连续且成对（2023-08-26午）"><a href="#这里利用的是栈的特性：先进先出，用来判断括号是否连续且成对（2023-08-26午）" class="headerlink" title="这里利用的是栈的特性：先进先出，用来判断括号是否连续且成对（2023/08/26午）"></a>这里利用的是栈的特性：先进先出，用来判断括号是否连续且成对（2023/08/26午）</h5></li>
<li><p>具体代码实现：</p>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">    * 括号匹配问题</span><br><span class="hljs-comment">    *</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@param</span> s</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">    */</span><br>   <span class="hljs-keyword">static</span> <span class="hljs-type">boolean</span> <span class="hljs-title function_">isValid</span><span class="hljs-params">(String s)</span> &#123;<br>       <span class="hljs-keyword">if</span> (s.length() &lt;= <span class="hljs-number">1</span>) &#123;<br>           <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;<br>       &#125;<br>       <span class="hljs-comment">// 1.构造HashMap</span><br>       Map&lt;Character, Character&gt; smap = <span class="hljs-keyword">new</span> <span class="hljs-title class_">HashMap</span>&lt;&gt;();<br>       smap.put(<span class="hljs-string">&#x27;(&#x27;</span>, <span class="hljs-string">&#x27;)&#x27;</span>);<br>       smap.put(<span class="hljs-string">&#x27;&#123;&#x27;</span>, <span class="hljs-string">&#x27;&#125;&#x27;</span>);<br>       smap.put(<span class="hljs-string">&#x27;[&#x27;</span>, <span class="hljs-string">&#x27;]&#x27;</span>);<br>       <span class="hljs-comment">// 2.构造栈</span><br>       Stack&lt;Character&gt; stack = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Stack</span>&lt;&gt;();<br>       <span class="hljs-comment">// 3.遍历字符串</span><br>       <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i &lt; s.length(); i++) &#123;<br>           <span class="hljs-type">char</span> <span class="hljs-variable">item</span> <span class="hljs-operator">=</span> s.charAt(i);<br>           <span class="hljs-comment">// 入栈左括号</span><br>           <span class="hljs-keyword">if</span> (smap.containsKey(item)) &#123;<br>               stack.push(item);<br>               <span class="hljs-comment">//检查右括号</span><br>           &#125; <span class="hljs-keyword">else</span> &#123;<br>               <span class="hljs-keyword">if</span> (stack.isEmpty()) &#123;<br>                   <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;<br>               &#125;<br><br>               <span class="hljs-type">Character</span> <span class="hljs-variable">left</span> <span class="hljs-operator">=</span> stack.pop();<br>               <span class="hljs-type">char</span> <span class="hljs-variable">rightChar</span> <span class="hljs-operator">=</span> smap.get(left);<br>               <span class="hljs-keyword">if</span> (rightChar != item) &#123;<br>                   <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;<br>               &#125;<br>           &#125;<br>       &#125;<br>       <span class="hljs-comment">//</span><br>       <span class="hljs-keyword">return</span> stack.isEmpty();<br>   &#125;<br></code></pre></td></tr></table></figure>

<h4 id="最小栈"><a href="#最小栈" class="headerlink" title="最小栈"></a>最小栈</h4><ul>
<li><p>实现快速获取栈中的最小元素</p>
</li>
<li><p>我们设计这样一个数据结构：</p>
<ul>
<li><p>构建两个栈，一个栈存放入栈元素，另一个栈存放栈顶元素对应的此时栈中最小的元素</p>
</li>
<li><h5 id="即最小栈中的栈顶，始终维护栈中的最小元素（2023-08-26午）"><a href="#即最小栈中的栈顶，始终维护栈中的最小元素（2023-08-26午）" class="headerlink" title="即最小栈中的栈顶，始终维护栈中的最小元素（2023/08/26午）"></a>即最小栈中的栈顶，始终维护栈中的最小元素（2023/08/26午）</h5></li>
<li><p>具体代码如下：</p>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">* 最小栈</span><br><span class="hljs-comment">*/</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">MinStack</span> &#123;  <br>    <span class="hljs-keyword">private</span> Stack&lt;Integer&gt; stack;  <br>    <span class="hljs-keyword">private</span> Stack&lt;Integer&gt; minStack;  <br>  <br>    <span class="hljs-keyword">public</span> <span class="hljs-title function_">MinStack</span><span class="hljs-params">()</span> &#123;  <br>        stack = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Stack</span>&lt;&gt;();  <br>        minStack = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Stack</span>&lt;&gt;();  <br>    &#125;  <br>  <br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">push</span><span class="hljs-params">(<span class="hljs-type">int</span> x)</span> &#123;  <br>        stack.push(x);  <br>        <span class="hljs-keyword">if</span> (minStack.isEmpty() || x &lt;= minStack.peek()) &#123;  <br>            minStack.push(x);  <br>        &#125;  <br>    &#125;  <br>  <br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">pop</span><span class="hljs-params">()</span> &#123;  <br>        <span class="hljs-keyword">if</span> (stack.peek().equals(minStack.peek())) &#123;  <br>            minStack.pop();  <br>        &#125;  <br>        stack.pop();  <br>    &#125;  <br>  <br>    <span class="hljs-keyword">public</span> <span class="hljs-type">int</span> <span class="hljs-title function_">top</span><span class="hljs-params">()</span> &#123;  <br>        <span class="hljs-keyword">return</span> stack.peek();  <br>    &#125;  <br>  <br>    <span class="hljs-keyword">public</span> <span class="hljs-type">int</span> <span class="hljs-title function_">getMin</span><span class="hljs-params">()</span> &#123;  <br>        <span class="hljs-keyword">return</span> minStack.peek();  <br>    &#125;  <br>&#125;<br></code></pre></td></tr></table></figure></li>
</ul>
<h4 id="最大栈"><a href="#最大栈" class="headerlink" title="最大栈"></a>最大栈</h4><ul>
<li><h5 id="思路与最小栈基本一致，具体代码如下："><a href="#思路与最小栈基本一致，具体代码如下：" class="headerlink" title="思路与最小栈基本一致，具体代码如下："></a>思路与最小栈基本一致，具体代码如下：</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">* 最大栈</span><br><span class="hljs-comment">*/</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">MaxStack</span> &#123;  <br>    <span class="hljs-keyword">private</span> Stack&lt;Integer&gt; stack;  <br>    <span class="hljs-keyword">private</span> Stack&lt;Integer&gt; maxStack;  <br>  <br>    <span class="hljs-keyword">public</span> <span class="hljs-title function_">MaxStack</span><span class="hljs-params">()</span> &#123;  <br>        stack = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Stack</span>&lt;&gt;();  <br>        maxStack = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Stack</span>&lt;&gt;();  <br>    &#125;  <br>  <br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">push</span><span class="hljs-params">(<span class="hljs-type">int</span> x)</span> &#123;  <br>        stack.push(x);  <br>        <span class="hljs-keyword">if</span> (maxStack.isEmpty() || x &gt;= maxStack.peek()) &#123;  <br>            maxStack.push(x);  <br>        &#125;  <br>    &#125;  <br>  <br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">pop</span><span class="hljs-params">()</span> &#123;  <br>        <span class="hljs-keyword">if</span> (stack.peek().equals(maxStack.peek())) &#123;  <br>            maxStack.pop();  <br>        &#125;  <br>        stack.pop();  <br>    &#125;  <br>  <br>    <span class="hljs-keyword">public</span> <span class="hljs-type">int</span> <span class="hljs-title function_">top</span><span class="hljs-params">()</span> &#123;  <br>        <span class="hljs-keyword">return</span> stack.peek();  <br>    &#125;  <br>  <br>    <span class="hljs-keyword">public</span> <span class="hljs-type">int</span> <span class="hljs-title function_">getMax</span><span class="hljs-params">()</span> &#123;  <br>        <span class="hljs-keyword">return</span> maxStack.peek();  <br>    &#125;  <br>&#125;<br></code></pre></td></tr></table></figure>

<ul>
<li>解释：<ul>
<li>对于实现最大栈的pop()方法：</li>
<li>当你从主栈中弹出一个元素，你会检查<strong>这个元素是否是辅助栈的栈顶元素</strong>。如果是，那么辅助栈的栈顶元素会被弹出。无论这个元素是否是辅助栈的栈顶元素，主栈的元素都会被弹出。</li>
<li>这样做的目的是为了<strong>维护辅助栈的栈顶元素始终是主栈当前的最大元素</strong>。当主栈的元素被弹出时，如果它不是最大的元素（即它不是辅助栈的栈顶元素），那么辅助栈的栈顶元素（即当前的最大元素）就不会被弹出。</li>
<li>因此，通过这种方式，辅助栈可以持续地维护主栈中的最大值。</li>
</ul>
</li>
<li>实在理不清过程的，<strong>强烈建议画个图</strong>，很<strong>直观</strong>，<strong>一目了然</strong>，尤其是栈、队列这种数据结构<strong>（2023/11/10晚）</strong></li>
</ul>
<p><img src="https://gitee.com/deng-2022/pictures/raw/master/images/image-20231110193335528.png" alt="image-20231110193335528"></p>
<h3 id="通关（过关挑战）-2"><a href="#通关（过关挑战）-2" class="headerlink" title="通关（过关挑战）"></a>通关（过关挑战）</h3><ul>
<li>这是要求：</li>
</ul>
<figure class="highlight dns"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs dns">给你一个栈，依次入栈<span class="hljs-number">1 2 3 4</span>，请问以下出栈顺序可能出现吗？为什么？<br></code></pre></td></tr></table></figure>

<ul>
<li><h5 id="我们解释两个例子吧：（2023-08-26午）"><a href="#我们解释两个例子吧：（2023-08-26午）" class="headerlink" title="我们解释两个例子吧：（2023/08/26午）"></a>我们解释两个例子吧：（2023/08/26午）</h5></li>
</ul>
<figure class="highlight tap"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs tap">出栈顺序：2<span class="hljs-number"> 3 </span>4<span class="hljs-number"> 1 </span>√<br>先出栈2，必然是1已经入栈了，再入栈3、出栈3，入栈4、出栈4，最后出栈1，没有问题<br></code></pre></td></tr></table></figure>

<figure class="highlight tap"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs tap">出栈顺序：1<span class="hljs-number"> 4 </span>2<span class="hljs-number"> 3 </span>×<br>先入栈1、出栈1，要出栈4，必然是2<span class="hljs-number"> 3 </span>4依次入栈了，出栈4之后的出栈顺序只能是3 2，而不可能是2 3，故错误<br></code></pre></td></tr></table></figure>

<ul>
<li>多做这样的例子，你就能彻底明白栈这个数据结构了：<strong>先进先出</strong></li>
</ul>
<h2 id="队列和Hash"><a href="#队列和Hash" class="headerlink" title="队列和Hash"></a>队列和Hash</h2><h3 id="基础知识（青铜挑战）-3"><a href="#基础知识（青铜挑战）-3" class="headerlink" title="基础知识（青铜挑战）"></a>基础知识（青铜挑战）</h3><h4 id="Hash"><a href="#Hash" class="headerlink" title="Hash"></a>Hash</h4><ul>
<li>Hash，又称散列，将任意长度的输入，通过散列算法，转换为固定长度的输出</li>
<li>映射、扩容机制</li>
<li>碰撞处理（Hash冲突）<ul>
<li><strong>开放寻址法</strong></li>
<li><strong>链地址法</strong></li>
<li>再哈希法（布隆过滤）</li>
<li>建立公共溢出区</li>
</ul>
</li>
</ul>
<h4 id="队列"><a href="#队列" class="headerlink" title="队列"></a>队列</h4><ul>
<li><h5 id="特点：先进先出（2023-09-03午）"><a href="#特点：先进先出（2023-09-03午）" class="headerlink" title="特点：先进先出（2023/09/03午）"></a>特点：先进先出（2023/09/03午）</h5></li>
<li><p>用链表实现队列</p>
</li>
<li><p>具体代码如下：</p>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">LinkQueue</span> &#123;<br>    <span class="hljs-keyword">private</span> Node front;<br>    <span class="hljs-keyword">private</span> Node rear;<br>    <span class="hljs-keyword">private</span> <span class="hljs-type">int</span> size;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-title function_">LinkQueue</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-built_in">this</span>.front = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Node</span>(<span class="hljs-number">0</span>);<br>        <span class="hljs-built_in">this</span>.rear = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Node</span>(<span class="hljs-number">0</span>);<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 入队</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">push</span><span class="hljs-params">(<span class="hljs-type">int</span> value)</span> &#123;<br>        <span class="hljs-comment">// 尾插法</span><br>        <span class="hljs-type">Node</span> <span class="hljs-variable">newNode</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Node</span>(value);<br>        <span class="hljs-type">Node</span> <span class="hljs-variable">temp</span> <span class="hljs-operator">=</span> front;<br>        <span class="hljs-keyword">while</span> (temp.next != <span class="hljs-literal">null</span>) &#123;<br>            temp = temp.next;<br>        &#125;<br>        temp.next = newNode;<br>        rear = newNode;<br>        size++;<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 出队</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-type">int</span> <span class="hljs-title function_">pull</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-comment">// 头删法</span><br>        <span class="hljs-keyword">if</span> (front.next == <span class="hljs-literal">null</span>) &#123;<br>            System.out.println(<span class="hljs-string">&quot;队列已空&quot;</span>);<br>        &#125;<br>        <span class="hljs-type">Node</span> <span class="hljs-variable">firstNode</span> <span class="hljs-operator">=</span> front.next;<br>        front.next = firstNode.next;<br>        size--;<br>        <span class="hljs-keyword">return</span> firstNode.data;<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 遍历队列</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">traverse</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-type">Node</span> <span class="hljs-variable">temp</span> <span class="hljs-operator">=</span> front.next;<br>        <span class="hljs-keyword">while</span> (temp != <span class="hljs-literal">null</span>) &#123;<br>            System.out.print(temp.data + <span class="hljs-string">&quot;\t&quot;</span>);<br>            temp = temp.next;<br>        &#125;<br>    &#125;<br><br>    <span class="hljs-keyword">static</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Node</span> &#123;<br>        <span class="hljs-keyword">public</span> <span class="hljs-type">int</span> data;<br>        <span class="hljs-keyword">public</span> Node next;<br><br>        <span class="hljs-keyword">public</span> <span class="hljs-title function_">Node</span><span class="hljs-params">(<span class="hljs-type">int</span> data)</span> &#123;<br>            <span class="hljs-built_in">this</span>.data = data;<br>        &#125;<br>    &#125;<br><br>    <span class="hljs-comment">//测试main方法</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> &#123;<br>        <span class="hljs-type">LinkQueue</span> <span class="hljs-variable">linkQueue</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">LinkQueue</span>();<br>        linkQueue.push(<span class="hljs-number">1</span>);<br>        linkQueue.push(<span class="hljs-number">2</span>);<br>        linkQueue.push(<span class="hljs-number">3</span>);<br>        System.out.println(<span class="hljs-string">&quot;第一个出队的元素为:&quot;</span> + linkQueue.pull());<br>        System.out.println(<span class="hljs-string">&quot;队列中的元素为:&quot;</span>);<br>        linkQueue.traverse();<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h3 id="实战训练（白银挑战）-3"><a href="#实战训练（白银挑战）-3" class="headerlink" title="实战训练（白银挑战）"></a>实战训练（白银挑战）</h3><h4 id="用栈实现队列"><a href="#用栈实现队列" class="headerlink" title="用栈实现队列"></a>用栈实现队列</h4><ul>
<li><p>情景：使用栈实现队列</p>
</li>
<li><h5 id="实现思路：（2023-09-03午）"><a href="#实现思路：（2023-09-03午）" class="headerlink" title="实现思路：（2023/09/03午）"></a>实现思路：（2023/09/03午）</h5><ul>
<li>利用栈先进后出的特性，首先一个栈inStack将元素压栈，<strong>它负责 “进” 元素</strong></li>
<li>inStack再依次出栈，另一个栈outStack负责将出栈的元素压栈，<strong>它负责 “出” 元素</strong></li>
</ul>
</li>
<li><p>具体代码如下：</p>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 用栈实现队列</span><br><span class="hljs-comment"> */</span><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">MyQueue</span> &#123;<br>    Deque&lt;Integer&gt; inStack;<br>    Deque&lt;Integer&gt; outStack;<br>    <span class="hljs-keyword">public</span> <span class="hljs-title function_">MyQueue</span><span class="hljs-params">()</span> &#123;<br>        inStack = <span class="hljs-keyword">new</span> <span class="hljs-title class_">LinkedList</span>&lt;Integer&gt;();<br>        outStack = <span class="hljs-keyword">new</span> <span class="hljs-title class_">LinkedList</span>&lt;Integer&gt;();<br>    &#125;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">push</span><span class="hljs-params">(<span class="hljs-type">int</span> x)</span> &#123;<br>        inStack.push(x);<br>    &#125;<br>    <span class="hljs-keyword">public</span> <span class="hljs-type">int</span> <span class="hljs-title function_">pop</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">if</span> (outStack.isEmpty()) &#123;<br>            in2out();<br>        &#125;<br>        <span class="hljs-keyword">return</span> outStack.pop();<br>    &#125;<br>    <span class="hljs-keyword">public</span> <span class="hljs-type">int</span> <span class="hljs-title function_">peek</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">if</span> (outStack.isEmpty()) &#123;<br>            in2out();<br>        &#125;<br>        <span class="hljs-keyword">return</span> outStack.peek();<br>    &#125;<br>    <span class="hljs-keyword">public</span> <span class="hljs-type">boolean</span> <span class="hljs-title function_">empty</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> inStack.isEmpty() &amp;&amp; outStack.isEmpty();<br>    &#125;<br>    <br>    <span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">in2out</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">while</span> (!inStack.isEmpty()) &#123;<br>            outStack.push(inStack.pop());<br>        &#125;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h4 id="用队列实现栈"><a href="#用队列实现栈" class="headerlink" title="用队列实现栈"></a>用队列实现栈</h4><ul>
<li><h5 id="实现思路：（2023-09-03午）-1"><a href="#实现思路：（2023-09-03午）-1" class="headerlink" title="实现思路：（2023/09/03午）"></a>实现思路：（2023/09/03午）</h5><ul>
<li>用两个队列实现栈可以这样做：假设你有队列A和队列B，每次push一个元素，都放入队列A中</li>
<li>当你想要pop一个元素时，先将队列A中的元素依次出队并入队到队列B中，直到队列A中只剩下一个元素，然后将这个元素出队</li>
<li>这样，你就模拟了栈后进先出的特性</li>
<li>当你继续执行push操作时，将元素放入非空的队列（此时是队列A）</li>
<li>而当你想要pop操作时，将除队列A中最后一个元素外的所有元素从队列A出队并依次入队到队列B中，再将队列A中剩下的最后一个元素出队</li>
<li>这样，可以保持栈的后进先出的特性，并且实现了用两个队列来模拟栈的功能</li>
<li>需要注意的是，每次pop操作后，需要交换队列A和队列B的角色，以便下一次的操作</li>
</ul>
</li>
<li><p>上面的思路是正确的，但是没有把核心思想说清楚，看的是云里雾里，<strong>实现思路如下：（2023/11/10晚）</strong></p>
</li>
</ul>
<blockquote>
<ul>
<li><p><strong>初始化阶段</strong>：创建两个队列，<code>queue1</code>和<code>queue2</code>。这两个队列开始时都是空的。<code>topElement</code>变量用来跟踪当前栈顶的元素。</p>
</li>
<li><p><strong>push操作</strong>：当执行push操作时，将新元素添加到<code>queue1</code>的尾部。同时，因为这个新元素就是当前的栈顶元素，所以更新<code>topElement</code>为这个新元素。</p>
</li>
<li><p><strong>pop操作</strong>：当执行pop操作时，首先将<code>queue1</code>中的所有元素（除了最后一个）依次弹出并添加到<code>queue2</code>中。这样做的效果是，<code>queue2</code>中的元素就是<code>queue1</code>中元素的逆序，即最后一个进入的元素现在成为了第一个。然后，将<code>queue1</code>和<code>queue2</code>交换，使得<code>queue2</code>成为了主要的队列。最后，从<code>queue2</code>中弹出的最后一个元素就是原来的栈顶元素，返回这个元素。</p>
</li>
<li><p><strong>peek操作</strong>：这个操作只需要返回当前的<code>topElement</code>即可，不需要进行任何操作。</p>
</li>
<li><p><strong>isEmpty操作</strong>：检查<code>queue1</code>是否为空，如果为空，那么栈就是空的，返回true；否则返回false。</p>
</li>
<li><h5 id="核心就是：两队列交换，queue1始终负责入栈，queue始终负责出栈"><a href="#核心就是：两队列交换，queue1始终负责入栈，queue始终负责出栈" class="headerlink" title="核心就是：两队列交换，queue1始终负责入栈，queue始终负责出栈"></a>核心就是：两队列交换，queue1始终负责入栈，queue始终负责出栈</h5></li>
</ul>
</blockquote>
<ul>
<li>具体代码如下：</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 用队列实现栈</span><br><span class="hljs-comment"> */</span><br>  <br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">StackUsingQueues</span> &#123;  <br>    <span class="hljs-keyword">private</span> Queue&lt;Integer&gt; queue1;  <br>    <span class="hljs-keyword">private</span> Queue&lt;Integer&gt; queue2;  <br>    <span class="hljs-keyword">private</span> <span class="hljs-type">int</span> topElement;  <br>  <br>    <span class="hljs-keyword">public</span> <span class="hljs-title function_">StackUsingQueues</span><span class="hljs-params">()</span> &#123;  <br>        queue1 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">LinkedList</span>&lt;&gt;();  <br>        queue2 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">LinkedList</span>&lt;&gt;();  <br>    &#125;  <br>  <br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">push</span><span class="hljs-params">(<span class="hljs-type">int</span> x)</span> &#123;  <br>        queue1.add(x);  <br>        topElement = x;  <br>    &#125;  <br>  <br>    <span class="hljs-keyword">public</span> <span class="hljs-type">int</span> <span class="hljs-title function_">pop</span><span class="hljs-params">()</span> &#123;  <br>        <span class="hljs-keyword">while</span>(queue1.size() &gt; <span class="hljs-number">1</span>) &#123;  <br>            queue2.add(queue1.remove());  <br>        &#125;  <br>  <br>        <span class="hljs-type">int</span> <span class="hljs-variable">poppedElement</span> <span class="hljs-operator">=</span> queue1.remove();  <br>        Queue&lt;Integer&gt; temp = queue1;  <br>        queue1 = queue2;  <br>        queue2 = temp;  <br>        <span class="hljs-keyword">return</span> poppedElement;  <br>    &#125;  <br>  <br>    <span class="hljs-keyword">public</span> <span class="hljs-type">int</span> <span class="hljs-title function_">peek</span><span class="hljs-params">()</span> &#123;  <br>        <span class="hljs-keyword">return</span> topElement;  <br>    &#125;  <br>  <br>    <span class="hljs-keyword">public</span> <span class="hljs-type">boolean</span> <span class="hljs-title function_">isEmpty</span><span class="hljs-params">()</span> &#123;  <br>        <span class="hljs-keyword">return</span> queue1.isEmpty();  <br>    &#125;  <br>&#125;<br></code></pre></td></tr></table></figure>

<ul>
<li>还是看不明白的，<strong>强烈建议画个图</strong>，一目了然，就算忘了也能快速回忆起来：<strong>（2023/11/10晚）</strong></li>
</ul>
<p><img src="https://gitee.com/deng-2022/pictures/raw/master/images/image-20231110200907461.png" alt="image-20231110200907461"></p>
<h4 id="两数之和"><a href="#两数之和" class="headerlink" title="两数之和"></a>两数之和</h4><ul>
<li>这题我之前做过，力扣里为数不多做过的题</li>
<li>情景：</li>
</ul>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs makefile"><span class="hljs-section">输入:nums = [2,7,11,15], target = 9</span><br><span class="hljs-section">输出:[0,1]</span><br><span class="hljs-section">解释:2 + 7 = 9, 返回二者下标</span><br></code></pre></td></tr></table></figure>

<ul>
<li>解决思路有三种，这里统一讲：<ul>
<li>遍历数组，依次<strong>两两相加</strong>，判断是否等于 target。缺点：双层循环嵌套 —&gt; <strong>时间复杂度大</strong></li>
<li>建立一张Hash表，存储 target - x，存在即返回，不存在即存储 x，继续遍历 —&gt; <strong>遍历过的数字已经存储到map中了，减少了遍历元素的个数</strong></li>
<li>排序数组，将<strong>二层循环改为二分查找</strong></li>
</ul>
</li>
<li>具体代码如下：</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-type">int</span>[] twoSum(<span class="hljs-type">int</span>[] nums, <span class="hljs-type">int</span> target) &#123;<br>      <span class="hljs-type">int</span> <span class="hljs-variable">n</span> <span class="hljs-operator">=</span> nums.length;<br>      <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i &lt; n; ++i) &#123;<br>          <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">j</span> <span class="hljs-operator">=</span> i + <span class="hljs-number">1</span>; j &lt; n; ++j) &#123;<br>              <span class="hljs-keyword">if</span> (nums[i] + nums[j] == target) &#123;<br>                  <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">int</span>[]&#123;i, j&#125;;<br>              &#125;<br>          &#125;<br>      &#125;<br>      <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">int</span>[<span class="hljs-number">0</span>];<br>  &#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-type">int</span>[] twoSum3(<span class="hljs-type">int</span>[] nums, <span class="hljs-type">int</span> target) &#123;<br>     <span class="hljs-comment">// 数组元素值 -&gt; 对应下标</span><br>     HashMap&lt;Integer, Integer&gt; map = <span class="hljs-keyword">new</span> <span class="hljs-title class_">HashMap</span>&lt;&gt;();<br>     <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i &lt; nums.length; i++) &#123;<br>         <span class="hljs-keyword">if</span> (map.containsKey(target - nums[i])) &#123;<br>             <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">int</span>[]&#123;i, map.get(target - nums[i])&#125;;<br>         &#125;<br>         map.put(nums[i], i);<br>     &#125;<br>     <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">int</span>[<span class="hljs-number">0</span>];<br> &#125;<br></code></pre></td></tr></table></figure>

<h4 id="三数之和"><a href="#三数之和" class="headerlink" title="三数之和"></a>三数之和</h4><h2 id="二叉树遍历（层数优先）"><a href="#二叉树遍历（层数优先）" class="headerlink" title="二叉树遍历（层数优先）"></a>二叉树遍历（层数优先）</h2><h3 id="基础知识（青铜挑战）-4"><a href="#基础知识（青铜挑战）-4" class="headerlink" title="基础知识（青铜挑战）"></a>基础知识（青铜挑战）</h3><ul>
<li>了解二叉树的基础知识</li>
</ul>
<h3 id="实战训练（白银挑战）-4"><a href="#实战训练（白银挑战）-4" class="headerlink" title="实战训练（白银挑战）"></a>实战训练（白银挑战）</h3><h4 id="简单的层序遍历"><a href="#简单的层序遍历" class="headerlink" title="简单的层序遍历"></a>简单的层序遍历</h4><ul>
<li>基本的层序遍历思路很清晰：<ul>
<li>给你一个二叉树根节点，你需要创建一个队列 queue 来遍历节点，一个链表 list 来存储节点的数据域，即值</li>
<li>首先将根节点入队</li>
<li>队列 queue 出队，将该节点值存入 list，再依次将左右孩子节点入队</li>
<li>重复以上操作，每个节点出对后，都存储该节点值到 list 中，再依次将左右孩子节点入队，直到队列 queue为空</li>
<li>这样得到的数据链表 list 就是按层序遍历的顺序得到的</li>
</ul>
</li>
<li><h5 id="具体代码如下：（2023-09-09午）"><a href="#具体代码如下：（2023-09-09午）" class="headerlink" title="具体代码如下：（2023/09/09午）"></a>具体代码如下：（2023/09/09午）</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> List&lt;Integer&gt; <span class="hljs-title function_">simpleLevelOrder</span><span class="hljs-params">(TreeNode root)</span> &#123;<br>    <span class="hljs-keyword">if</span> (root == <span class="hljs-literal">null</span>) &#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayList</span>&lt;Integer&gt;();<br>    &#125;<br><br>    List&lt;Integer&gt; res = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayList</span>&lt;Integer&gt;();<br>    LinkedList&lt;TreeNode&gt; queue = <span class="hljs-keyword">new</span> <span class="hljs-title class_">LinkedList</span>&lt;TreeNode&gt;();<br>    <span class="hljs-comment">//将根节点放入队列中，然后不断遍历队列</span><br>    queue.add(root);<br>    <span class="hljs-comment">//有多少元素执行多少次</span><br>    <span class="hljs-keyword">while</span> (queue.size() &gt; <span class="hljs-number">0</span>) &#123;<br>        <span class="hljs-comment">//获取当前队列的长度，这个长度相当于 当前这一层的节点个数</span><br>        <span class="hljs-type">TreeNode</span> <span class="hljs-variable">t</span> <span class="hljs-operator">=</span> queue.remove();<br>        res.add(t.val);<br>        <span class="hljs-keyword">if</span> (t.left != <span class="hljs-literal">null</span>) &#123;<br>            queue.add(t.left);<br>        &#125;<br>        <span class="hljs-keyword">if</span> (t.right != <span class="hljs-literal">null</span>) &#123;<br>            queue.add(t.right);<br>        &#125;<br>    &#125;<br>    <span class="hljs-keyword">return</span> res;<br>&#125;<br></code></pre></td></tr></table></figure>

<h4 id="层序遍历分层"><a href="#层序遍历分层" class="headerlink" title="层序遍历分层"></a>层序遍历分层</h4><ul>
<li>层序遍历我们做到了，这里添加一个要求：对层序遍历的节点值分层处理，即二叉树每层的节点值分别存放进一个链表 list 中</li>
<li>这个代码怎么写呢？很简单的，按这个思路走：<ul>
<li>我们之前层序遍历时，每出队一个节点，都把其值存入 list 链表中，然后入队其孩子节点</li>
<li>在开始出队某一层的节点时，此时队列的节点数，就是二叉树这一层的节点数</li>
<li>那我们根据可以某时刻队列容量来遍历队列，将这层的节点全部出队，并且把节点值存入该层独有的 list 中</li>
<li>当然了，每个节点出队后，要将自己的孩子节点依次入队</li>
<li>这样，当队列为空时，我们得到了各层的节点链表 list，返回一个包含各层 list 的 list 即可</li>
</ul>
</li>
<li><h5 id="具体代码如下：（2023-09-09晚）"><a href="#具体代码如下：（2023-09-09晚）" class="headerlink" title="具体代码如下：（2023/09/09晚）"></a>具体代码如下：（2023/09/09晚）</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> List&lt;List&lt;Integer&gt;&gt; <span class="hljs-title function_">level102Order</span><span class="hljs-params">(TreeNode root)</span> &#123;<br>      <span class="hljs-keyword">if</span> (root == <span class="hljs-literal">null</span>) &#123;<br>          <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayList</span>&lt;List&lt;Integer&gt;&gt;();<br>      &#125;<br><br>      List&lt;List&lt;Integer&gt;&gt; res = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayList</span>&lt;List&lt;Integer&gt;&gt;();<br>      LinkedList&lt;TreeNode&gt; queue = <span class="hljs-keyword">new</span> <span class="hljs-title class_">LinkedList</span>&lt;TreeNode&gt;();<br>      <span class="hljs-comment">//将根节点放入队列中，然后不断遍历队列</span><br>      queue.add(root);<br>      <span class="hljs-keyword">while</span> (queue.size() &gt; <span class="hljs-number">0</span>) &#123;<br>          <span class="hljs-comment">//获取当前队列的长度，这个长度相当于 当前这一层的节点个数</span><br>          <span class="hljs-type">int</span> <span class="hljs-variable">size</span> <span class="hljs-operator">=</span> queue.size();<br>          ArrayList&lt;Integer&gt; tmp = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayList</span>&lt;Integer&gt;();<br>          <span class="hljs-comment">//将队列中的元素都拿出来(也就是获取这一层的节点)，放到临时list中</span><br>          <span class="hljs-comment">//如果节点的左/右子树不为空，也放入队列中</span><br>          <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i &lt; size; ++i) &#123;<br>              <span class="hljs-type">TreeNode</span> <span class="hljs-variable">t</span> <span class="hljs-operator">=</span> queue.remove();<br>              tmp.add(t.val);<br>              <span class="hljs-keyword">if</span> (t.left != <span class="hljs-literal">null</span>) &#123;<br>                  queue.add(t.left);<br>              &#125;<br>              <span class="hljs-keyword">if</span> (t.right != <span class="hljs-literal">null</span>) &#123;<br>                  queue.add(t.right);<br>              &#125;<br>          &#125;<br>          <span class="hljs-comment">//将临时list加入最终返回结果中</span><br>          res.add(tmp);<br>      &#125;<br>      <span class="hljs-keyword">return</span> res;<br>  &#125;<br></code></pre></td></tr></table></figure>

<h4 id="自底向上的层序遍历"><a href="#自底向上的层序遍历" class="headerlink" title="自底向上的层序遍历"></a>自底向上的层序遍历</h4><ul>
<li><p>在前面学习的基础上，实现这个要求就很简单了</p>
</li>
<li><h5 id="在拿到各层节点值的-list-后，按头插的方式，插入链表-list-中，就实现了自底向上的层序遍历了（2023-09-09晚）"><a href="#在拿到各层节点值的-list-后，按头插的方式，插入链表-list-中，就实现了自底向上的层序遍历了（2023-09-09晚）" class="headerlink" title="在拿到各层节点值的 list 后，按头插的方式，插入链表 list 中，就实现了自底向上的层序遍历了（2023/09/09晚）"></a>在拿到各层节点值的 list 后，按头插的方式，插入链表 list 中，就实现了自底向上的层序遍历了（2023/09/09晚）</h5></li>
<li><h5 id="具体代码如下：-8"><a href="#具体代码如下：-8" class="headerlink" title="具体代码如下："></a>具体代码如下：</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> List&lt;List&lt;Integer&gt;&gt; <span class="hljs-title function_">levelOrderBottom</span><span class="hljs-params">(TreeNode root)</span> &#123;<br>    List&lt;List&lt;Integer&gt;&gt; levelOrder = <span class="hljs-keyword">new</span> <span class="hljs-title class_">LinkedList</span>&lt;List&lt;Integer&gt;&gt;();<br>    <span class="hljs-keyword">if</span> (root == <span class="hljs-literal">null</span>) &#123;<br>        <span class="hljs-keyword">return</span> levelOrder;<br>    &#125;<br>    Queue&lt;TreeNode&gt; queue = <span class="hljs-keyword">new</span> <span class="hljs-title class_">LinkedList</span>&lt;TreeNode&gt;();<br>    queue.offer(root);<br>    <span class="hljs-keyword">while</span> (!queue.isEmpty()) &#123;<br>        List&lt;Integer&gt; level = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayList</span>&lt;Integer&gt;();<br>        <span class="hljs-type">int</span> <span class="hljs-variable">size</span> <span class="hljs-operator">=</span> queue.size();<br>        <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i &lt; size; i++) &#123;<br>            <span class="hljs-type">TreeNode</span> <span class="hljs-variable">node</span> <span class="hljs-operator">=</span> queue.poll();<br>            level.add(node.val);<br>            <span class="hljs-type">TreeNode</span> <span class="hljs-variable">left</span> <span class="hljs-operator">=</span> node.left, right = node.right;<br>            <span class="hljs-keyword">if</span> (left != <span class="hljs-literal">null</span>) &#123;<br>                queue.offer(left);<br>            &#125;<br>            <span class="hljs-keyword">if</span> (right != <span class="hljs-literal">null</span>) &#123;<br>                queue.offer(right);<br>            &#125;<br>        &#125;<br>        levelOrder.add(<span class="hljs-number">0</span>, level);<span class="hljs-comment">//栈</span><br>    &#125;<br>    <span class="hljs-keyword">return</span> levelOrder;<br>&#125;<br></code></pre></td></tr></table></figure>

<h4 id="锯齿形层序遍历"><a href="#锯齿形层序遍历" class="headerlink" title="锯齿形层序遍历"></a>锯齿形层序遍历</h4><ul>
<li>情景：</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs 11">二叉树如下:<br>	 3<br>   /   \  <br>  8     9<br> / \   /<br>20  11 6<br>锯齿形遍历：3 9 8 20 11 6<br></code></pre></td></tr></table></figure>

<ul>
<li>在基本的层序遍历的基础上，如何实现锯齿形层序遍历？</li>
<li>这样的思路能否完成呢：其他条件不变，<strong>改变存放节点值的顺序</strong>如何？（2023/09/10午）</li>
<li>我们这里将 <strong>list 该换为 queue</strong> ，因为它可以提供了不同的入队方式</li>
<li>具体代码如下：</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> List&lt;List&lt;Integer&gt;&gt; <span class="hljs-title function_">zigzagLevelOrder2</span><span class="hljs-params">(TreeNode root)</span> &#123;<br>       <span class="hljs-comment">// 节点值列表</span><br>       List&lt;List&lt;Integer&gt;&gt; ans = <span class="hljs-keyword">new</span> <span class="hljs-title class_">LinkedList</span>&lt;&gt;();<br>       <span class="hljs-keyword">if</span> (root == <span class="hljs-literal">null</span>)<br>           <span class="hljs-keyword">return</span> ans;<br>       <span class="hljs-comment">// 队列，控制遍历节点顺序</span><br>       Deque&lt;TreeNode&gt; queue = <span class="hljs-keyword">new</span> <span class="hljs-title class_">LinkedList</span>&lt;&gt;();<br>       <span class="hljs-comment">// 根节点入队</span><br>       queue.offer(root);<br>       <span class="hljs-comment">// 控制存放节点值顺序</span><br>       <span class="hljs-type">boolean</span> <span class="hljs-variable">isOrderLeft</span> <span class="hljs-operator">=</span> <span class="hljs-literal">true</span>;<br>       <span class="hljs-keyword">while</span> (!queue.isEmpty()) &#123;<br>           <span class="hljs-comment">// 每层的节点值列表</span><br>           Deque&lt;Integer&gt; levelList = <span class="hljs-keyword">new</span> <span class="hljs-title class_">LinkedList</span>&lt;&gt;();<br>           <span class="hljs-comment">// 该层节点数</span><br>           <span class="hljs-type">int</span> <span class="hljs-variable">size</span> <span class="hljs-operator">=</span> queue.size();<br>           <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i &lt; size; i++) &#123;<br>               <span class="hljs-comment">// 出队，存值</span><br>               <span class="hljs-type">TreeNode</span> <span class="hljs-variable">poll</span> <span class="hljs-operator">=</span> queue.poll();<br>               <span class="hljs-keyword">if</span> (isOrderLeft) &#123;<br>                   levelList.addFirst(poll.val);<br>               &#125; <span class="hljs-keyword">else</span> &#123;<br>                   levelList.addLast(poll.val);<br>               &#125;<br>               <span class="hljs-comment">// 左右孩子节点入队</span><br>               <span class="hljs-keyword">if</span> (poll.left != <span class="hljs-literal">null</span>) &#123;<br>                   queue.add(poll.left);<br>               &#125;<br>               <span class="hljs-keyword">if</span> (poll.left != <span class="hljs-literal">null</span>) &#123;<br>                   queue.add(poll.right);<br>               &#125;<br>           &#125;<br>           ans.add(<span class="hljs-keyword">new</span> <span class="hljs-title class_">LinkedList</span>&lt;Integer&gt;(levelList));<br>           <span class="hljs-comment">// 变换存放节点值顺序</span><br>           isOrderLeft = !isOrderLeft;<br>       &#125;<br>       <span class="hljs-keyword">return</span> ans;<br>   &#125;<br></code></pre></td></tr></table></figure>

<h4 id="N叉数的层序遍历"><a href="#N叉数的层序遍历" class="headerlink" title="N叉数的层序遍历"></a>N叉数的层序遍历</h4><ul>
<li><h5 id="问题：将二叉树的情景改为N叉数，遍历结果也是很简单的，即将左右子节点改为若干子节点（2023-09-10午）"><a href="#问题：将二叉树的情景改为N叉数，遍历结果也是很简单的，即将左右子节点改为若干子节点（2023-09-10午）" class="headerlink" title="问题：将二叉树的情景改为N叉数，遍历结果也是很简单的，即将左右子节点改为若干子节点（2023/09/10午）"></a>问题：将二叉树的情景改为N叉数，遍历结果也是很简单的，即将左右子节点改为若干子节点（2023/09/10午）</h5></li>
<li><p>具体代码如下：</p>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> List&lt;List&lt;Integer&gt;&gt; <span class="hljs-title function_">nLevelOrder</span><span class="hljs-params">(NTreeNode root)</span> &#123;<br>      List&lt;List&lt;Integer&gt;&gt; ans = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayList</span>&lt;&gt;();<br>      Deque&lt;NTreeNode&gt; queue = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayDeque</span>&lt;&gt;();<br><br>      <span class="hljs-keyword">if</span> (root != <span class="hljs-literal">null</span>)<br>          queue.addLast(root);<br><br>      <span class="hljs-keyword">while</span> (!queue.isEmpty()) &#123;<br>          Deque&lt;NTreeNode&gt; next = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayDeque</span>&lt;&gt;();<br>          List&lt;Integer&gt; nd = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayList</span>&lt;&gt;();<br><br>          <span class="hljs-keyword">while</span> (!queue.isEmpty()) &#123;<br>              <span class="hljs-type">NTreeNode</span> <span class="hljs-variable">cur</span> <span class="hljs-operator">=</span> queue.pollFirst();<br>              nd.add(cur.val);<br>              <span class="hljs-comment">// 入队，将孩子节点依次入队</span><br>              <span class="hljs-keyword">for</span> (NTreeNode chd : cur.children) &#123;<br>                  <span class="hljs-keyword">if</span> (chd != <span class="hljs-literal">null</span>)<br>                      next.add(chd);<br>              &#125;<br>          &#125;<br>          queue = next;<br>          ans.add(nd);<br>      &#125;<br>      <span class="hljs-keyword">return</span> ans;<br>  &#125;<br></code></pre></td></tr></table></figure>

<ul>
<li>这里暂时没看懂为什么加了个 queue，日后解答</li>
</ul>
<h4 id="获取每一层的最大数（扩展）"><a href="#获取每一层的最大数（扩展）" class="headerlink" title="获取每一层的最大数（扩展）"></a>获取每一层的最大数（扩展）</h4><ul>
<li><h5 id="出队每层节点时，累加节点值，求平均值即可：（2023-09-11早）"><a href="#出队每层节点时，累加节点值，求平均值即可：（2023-09-11早）" class="headerlink" title="出队每层节点时，累加节点值，求平均值即可：（2023/09/11早）"></a>出队每层节点时，累加节点值，求平均值即可：（2023/09/11早）</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> List&lt;Double&gt; <span class="hljs-title function_">averageOfLevels</span><span class="hljs-params">(TreeNode root)</span> &#123;<br>        List&lt;Double&gt; res = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayList</span>&lt;&gt;();<br>        <span class="hljs-keyword">if</span> (root == <span class="hljs-literal">null</span>) <span class="hljs-keyword">return</span> res;<br>        <br>        Queue&lt;TreeNode&gt; list = <span class="hljs-keyword">new</span> <span class="hljs-title class_">LinkedList</span>&lt;&gt;();<br>        list.add(root);<br>        <br>        <span class="hljs-keyword">while</span> (list.size() != <span class="hljs-number">0</span>) &#123;<br>            <span class="hljs-type">int</span> <span class="hljs-variable">len</span> <span class="hljs-operator">=</span> list.size();<br>            <span class="hljs-type">double</span> <span class="hljs-variable">sum</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;<br>            <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i &lt; len; i++) &#123;<br>                <span class="hljs-type">TreeNode</span> <span class="hljs-variable">node</span> <span class="hljs-operator">=</span> list.poll();<br>                <br>                sum += node.val;<br>                <span class="hljs-keyword">if</span> (node.left != <span class="hljs-literal">null</span>) list.add(node.left);<br>                <span class="hljs-keyword">if</span> (node.right != <span class="hljs-literal">null</span>) list.add(node.right);<br>            &#125;<br>            res.add(sum / len);<br>        &#125;<br>        <span class="hljs-keyword">return</span> res;<br>    &#125;<br></code></pre></td></tr></table></figure>

<h4 id="获取每一层的平均数（扩展）"><a href="#获取每一层的平均数（扩展）" class="headerlink" title="获取每一层的平均数（扩展）"></a>获取每一层的平均数（扩展）</h4><ul>
<li>出队节点时，比较节点值，求取最大值即可：</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> List&lt;Double&gt; <span class="hljs-title function_">largestValues2</span><span class="hljs-params">(TreeNode root)</span> &#123;<br>      List&lt;Double&gt; res = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayList</span>&lt;&gt;();<br>      <span class="hljs-keyword">if</span> (root == <span class="hljs-literal">null</span>) <span class="hljs-keyword">return</span> res;<br><br>      Queue&lt;TreeNode&gt; list = <span class="hljs-keyword">new</span> <span class="hljs-title class_">LinkedList</span>&lt;&gt;();<br>      list.add(root);<br><br>      <span class="hljs-keyword">while</span> (list.size() != <span class="hljs-number">0</span>) &#123;<br>          <span class="hljs-type">int</span> <span class="hljs-variable">len</span> <span class="hljs-operator">=</span> list.size();<br>          <span class="hljs-type">double</span> <span class="hljs-variable">max</span> <span class="hljs-operator">=</span> Double.MIN_VALUE;<br>          <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i &lt; len; i++) &#123;<br>              <span class="hljs-type">TreeNode</span> <span class="hljs-variable">node</span> <span class="hljs-operator">=</span> list.poll();<br><br>              max = Double.max(max, node.val);<br>              <span class="hljs-keyword">if</span> (node.left != <span class="hljs-literal">null</span>) list.add(node.left);<br>              <span class="hljs-keyword">if</span> (node.right != <span class="hljs-literal">null</span>) list.add(node.right);<br>          &#125;<br>          res.add(max);<br>      &#125;<br>      <span class="hljs-keyword">return</span> res;<br>  &#125;<br></code></pre></td></tr></table></figure>

<h4 id="获取二叉树的右视图（扩展）"><a href="#获取二叉树的右视图（扩展）" class="headerlink" title="获取二叉树的右视图（扩展）"></a>获取二叉树的右视图（扩展）</h4><ul>
<li>统计每一层出队的最后一个节点即可</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> List&lt;Integer&gt; <span class="hljs-title function_">rightSideView</span><span class="hljs-params">(TreeNode root)</span> &#123;<br>        List&lt;Integer&gt; res = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayList</span>&lt;&gt;();<br>        <span class="hljs-keyword">if</span> (root == <span class="hljs-literal">null</span>) &#123;<br>            <span class="hljs-keyword">return</span> res;<br>        &#125;<br>        Queue&lt;TreeNode&gt; queue = <span class="hljs-keyword">new</span> <span class="hljs-title class_">LinkedList</span>&lt;&gt;();<br>        queue.offer(root);<br>        <span class="hljs-keyword">while</span> (!queue.isEmpty()) &#123;<br>            <span class="hljs-type">int</span> <span class="hljs-variable">size</span> <span class="hljs-operator">=</span> queue.size();<br>            <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i &lt; size; i++) &#123;<br>                <span class="hljs-type">TreeNode</span> <span class="hljs-variable">node</span> <span class="hljs-operator">=</span> queue.poll();<br>                <span class="hljs-keyword">if</span> (node.left != <span class="hljs-literal">null</span>) &#123;<br>                    queue.offer(node.left);<br>                &#125;<br>                <span class="hljs-keyword">if</span> (node.right != <span class="hljs-literal">null</span>) &#123;<br>                    queue.offer(node.right);<br>                &#125;<br>                <span class="hljs-keyword">if</span> (i == size - <span class="hljs-number">1</span>) &#123;  <span class="hljs-comment">//将当前层的最后一个节点放入结果列表</span><br>                    res.add(node.val);<br>                &#125;<br>            &#125;<br>        &#125;<br>        <span class="hljs-keyword">return</span> res;<br>    &#125;<br></code></pre></td></tr></table></figure>

<h3 id="通关（过关挑战）-3"><a href="#通关（过关挑战）-3" class="headerlink" title="通关（过关挑战）"></a>通关（过关挑战）</h3><ul>
<li><h5 id="情景：获取一个二叉树的左视图（2023-09-12午）"><a href="#情景：获取一个二叉树的左视图（2023-09-12午）" class="headerlink" title="情景：获取一个二叉树的左视图（2023/09/12午）"></a>情景：获取一个二叉树的左视图（2023/09/12午）</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> List&lt;Integer&gt; <span class="hljs-title function_">rightSideView</span><span class="hljs-params">(TreeNode root)</span> &#123;<br>        List&lt;Integer&gt; res = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayList</span>&lt;&gt;();<br>        <span class="hljs-keyword">if</span> (root == <span class="hljs-literal">null</span>) &#123;<br>            <span class="hljs-keyword">return</span> res;<br>        &#125;<br>        Queue&lt;TreeNode&gt; queue = <span class="hljs-keyword">new</span> <span class="hljs-title class_">LinkedList</span>&lt;&gt;();<br>        queue.offer(root);<br>        <span class="hljs-keyword">while</span> (!queue.isEmpty()) &#123;<br>            <span class="hljs-type">int</span> <span class="hljs-variable">size</span> <span class="hljs-operator">=</span> queue.size();<br>            <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i &lt; size; i++) &#123;<br>                <span class="hljs-type">TreeNode</span> <span class="hljs-variable">node</span> <span class="hljs-operator">=</span> queue.poll();<br>                <span class="hljs-keyword">if</span> (node.left != <span class="hljs-literal">null</span>) &#123;<br>                    queue.offer(node.left);<br>                &#125;<br>                <span class="hljs-keyword">if</span> (node.right != <span class="hljs-literal">null</span>) &#123;<br>                    queue.offer(node.right);<br>                &#125;<br>                <span class="hljs-keyword">if</span> (i == <span class="hljs-number">0</span>) &#123;  <span class="hljs-comment">//将当前层的第一个节点放入结果列表</span><br>                    res.add(node.val);<br>                &#125;<br>            &#125;<br>        &#125;<br>        <span class="hljs-keyword">return</span> res;<br>    &#125;<br></code></pre></td></tr></table></figure>

<h2 id="二叉树遍历（深度优先）"><a href="#二叉树遍历（深度优先）" class="headerlink" title="二叉树遍历（深度优先）"></a>二叉树遍历（深度优先）</h2><h3 id="基础知识（青铜挑战）-5"><a href="#基础知识（青铜挑战）-5" class="headerlink" title="基础知识（青铜挑战）"></a>基础知识（青铜挑战）</h3><ul>
<li>理解递归思想：调用自己、压栈、明确终止条件</li>
</ul>
<h3 id="实战训练（白银挑战）-5"><a href="#实战训练（白银挑战）-5" class="headerlink" title="实战训练（白银挑战）"></a>实战训练（白银挑战）</h3><h4 id="递归实现二叉树的前、中、后序遍历"><a href="#递归实现二叉树的前、中、后序遍历" class="headerlink" title="递归实现二叉树的前、中、后序遍历"></a>递归实现二叉树的前、中、后序遍历</h4><ul>
<li><p>我的建议是直接把代码背下来</p>
</li>
<li><p>你当然可以尝试去理解，也不是很难想明白，但是不要钻牛角尖了，递归这玩意儿，很不友好</p>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">    * 前序遍历，将结果返回到list中</span><br><span class="hljs-comment">    *</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@param</span> root</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@param</span> res</span><br><span class="hljs-comment">    */</span><br><br>   <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">preOrder</span><span class="hljs-params">(TreeNode root, List&lt;Integer&gt; res)</span> &#123;<br>       <span class="hljs-keyword">if</span> (root == <span class="hljs-literal">null</span>) &#123;<br>           <span class="hljs-keyword">return</span>;<br>       &#125;<br>       res.add(root.val);<br>       preOrder(root.left, res);<br>       preOrder(root.right, res);<br>   &#125;<br><br>  <br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs java"> <span class="hljs-comment">/**</span><br><span class="hljs-comment">   * 中序遍历，将结果返回到list中</span><br><span class="hljs-comment">   *</span><br><span class="hljs-comment">   * <span class="hljs-doctag">@param</span> root</span><br><span class="hljs-comment">   * <span class="hljs-doctag">@param</span> res</span><br><span class="hljs-comment">   */</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">inOrder</span><span class="hljs-params">(TreeNode root, List&lt;Integer&gt; res)</span> &#123;<br>      <span class="hljs-keyword">if</span> (root == <span class="hljs-literal">null</span>) &#123;<br>          <span class="hljs-keyword">return</span>;<br>      &#125;<br>      inOrder(root.left, res);<br>      res.add(root.val);<br>      inOrder(root.right, res);<br>  &#125;<br><br><br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 后序遍历，将结果返回到list中</span><br><span class="hljs-comment"> *</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@param</span> root</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@param</span> res</span><br><span class="hljs-comment"> */</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">postOrder</span><span class="hljs-params">(TreeNode root, List&lt;Integer&gt; res)</span> &#123;<br>    <span class="hljs-keyword">if</span> (root == <span class="hljs-literal">null</span>) &#123;<br>        <span class="hljs-keyword">return</span>;<br>    &#125;<br>    postOrder(root.left, res);<br>    postOrder(root.right, res);<br>    res.add(root.val);<br>&#125;<br></code></pre></td></tr></table></figure>

<h4 id="迭代实现二叉树的前、中、后序遍历"><a href="#迭代实现二叉树的前、中、后序遍历" class="headerlink" title="迭代实现二叉树的前、中、后序遍历"></a>迭代实现二叉树的前、中、后序遍历</h4><ul>
<li><h5 id="前序遍历"><a href="#前序遍历" class="headerlink" title="前序遍历"></a>前序遍历</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> List&lt;Integer&gt; <span class="hljs-title function_">preOrderTraversal</span><span class="hljs-params">(TreeNode root)</span> &#123;<br>     List&lt;Integer&gt; res = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayList</span>&lt;Integer&gt;();<br>     <span class="hljs-keyword">if</span> (root == <span class="hljs-literal">null</span>) &#123;<br>         <span class="hljs-keyword">return</span> res;<br>     &#125;<br><br>     Deque&lt;TreeNode&gt; stack = <span class="hljs-keyword">new</span> <span class="hljs-title class_">LinkedList</span>&lt;TreeNode&gt;();<br>     <span class="hljs-type">TreeNode</span> <span class="hljs-variable">node</span> <span class="hljs-operator">=</span> root;<br>     <span class="hljs-keyword">while</span> (!stack.isEmpty() || node != <span class="hljs-literal">null</span>) &#123;<br>         <span class="hljs-keyword">while</span> (node != <span class="hljs-literal">null</span>) &#123;<br>             res.add(node.val);<br>             stack.push(node);<br>             node = node.left;<br>         &#125;<br><br>         node = stack.pop();<br>         node = node.right;<br>     &#125;<br>     <span class="hljs-keyword">return</span> res;<br> &#125;   <br></code></pre></td></tr></table></figure>

<ul>
<li><h5 id="中序遍历（2023-09-12午）"><a href="#中序遍历（2023-09-12午）" class="headerlink" title="中序遍历（2023/09/12午）"></a>中序遍历（2023/09/12午）</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> List&lt;Integer&gt; <span class="hljs-title function_">inorderTraversal</span><span class="hljs-params">(TreeNode root)</span> &#123;<br>        List&lt;Integer&gt; res = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayList</span>&lt;Integer&gt;();<br>        Deque&lt;TreeNode&gt; stack = <span class="hljs-keyword">new</span> <span class="hljs-title class_">LinkedList</span>&lt;TreeNode&gt;();<br><br>        <span class="hljs-keyword">while</span> (root != <span class="hljs-literal">null</span> || !stack.isEmpty()) &#123;<br>            <span class="hljs-keyword">while</span> (root != <span class="hljs-literal">null</span>) &#123;<br>                stack.push(root);<br>                root = root.left;<br>            &#125;<br><br>            root = stack.pop();<br>            res.add(root.val);<br>            root = root.right;<br>        &#125;<br>        <span class="hljs-keyword">return</span> res;<br>    &#125;<br></code></pre></td></tr></table></figure>

<ul>
<li><p>前序遍历和中序遍历的代码不复杂，还是注意：挺不好理解的，不要钻牛角尖，那么我们接下来看下后序遍历</p>
</li>
<li><h5 id="后序遍历"><a href="#后序遍历" class="headerlink" title="后序遍历"></a>后序遍历</h5></li>
<li><p>这个相较于前两个更难实现，但可以这样想：</p>
<ul>
<li>前序遍历的顺序是：中、左、右，我们简单该两个参数，实现遍历顺序为中、右、左</li>
<li>再将遍历结果reserve反转，不就得到左、右、中的遍历顺序了吗？</li>
</ul>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> List&lt;Integer&gt; <span class="hljs-title function_">postOrderTraversal</span><span class="hljs-params">(TreeNode root)</span> &#123;<br>      List&lt;Integer&gt; res = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayList</span>&lt;&gt;();<br>      <span class="hljs-keyword">if</span> (root == <span class="hljs-literal">null</span>) <span class="hljs-keyword">return</span> res;<br>      Stack&lt;TreeNode&gt; stack = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Stack</span>&lt;&gt;();<br>      <span class="hljs-type">TreeNode</span> <span class="hljs-variable">node</span> <span class="hljs-operator">=</span> root;<br><br>      <span class="hljs-keyword">while</span> (!stack.isEmpty() || node != <span class="hljs-literal">null</span>) &#123;<br>          <span class="hljs-keyword">while</span> (node != <span class="hljs-literal">null</span>) &#123;<br>              res.add(node.val);<br>              stack.push(node);<br>              node = node.right;<br>          &#125;<br>          node = stack.pop();<br>          node = node.left;<br>      &#125;<br>      Collections.reverse(res);<br>      <span class="hljs-keyword">return</span> res;<br>  &#125;<br></code></pre></td></tr></table></figure>

<h3 id="通关（过关挑战）-4"><a href="#通关（过关挑战）-4" class="headerlink" title="通关（过关挑战）"></a>通关（过关挑战）</h3><ul>
<li><h5 id="理解中序遍历的递归过程，挺难的，努努力吧（2023-09-12午）"><a href="#理解中序遍历的递归过程，挺难的，努努力吧（2023-09-12午）" class="headerlink" title="理解中序遍历的递归过程，挺难的，努努力吧（2023/09/12午）"></a>理解中序遍历的递归过程，挺难的，努努力吧（2023/09/12午）</h5></li>
</ul>
<h2 id="二叉树深度优先经典问题"><a href="#二叉树深度优先经典问题" class="headerlink" title="二叉树深度优先经典问题"></a>二叉树深度优先经典问题</h2><ul>
<li><h5 id="这块儿内容我个人认为非常抽象，虽然重要，但我还是决定先跳过这部分了，脑壳疼（2023-09-12午）"><a href="#这块儿内容我个人认为非常抽象，虽然重要，但我还是决定先跳过这部分了，脑壳疼（2023-09-12午）" class="headerlink" title="这块儿内容我个人认为非常抽象，虽然重要，但我还是决定先跳过这部分了，脑壳疼（2023/09/12午）"></a>这块儿内容我个人认为非常抽象，虽然重要，但我还是决定先跳过这部分了，脑壳疼（2023/09/12午）</h5></li>
</ul>
<h3 id="最大深度问题"><a href="#最大深度问题" class="headerlink" title="最大深度问题"></a>最大深度问题</h3><h3 id="判断平衡树"><a href="#判断平衡树" class="headerlink" title="判断平衡树"></a>判断平衡树</h3><h3 id="最小深度"><a href="#最小深度" class="headerlink" title="最小深度"></a>最小深度</h3><h3 id="N叉数的最大深度"><a href="#N叉数的最大深度" class="headerlink" title="N叉数的最大深度"></a>N叉数的最大深度</h3><h2 id="二分查找和搜索数"><a href="#二分查找和搜索数" class="headerlink" title="二分查找和搜索数"></a>二分查找和搜索数</h2><h3 id="基础知识（青铜挑战）-6"><a href="#基础知识（青铜挑战）-6" class="headerlink" title="基础知识（青铜挑战）"></a>基础知识（青铜挑战）</h3><ul>
<li>了解顺序查找、以及二分查找的简单实现。经典的顺序查找：</li>
</ul>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs"><br></code></pre></td></tr></table></figure>

<h4 id="简单的二分查找-1"><a href="#简单的二分查找-1" class="headerlink" title="简单的二分查找"></a>简单的二分查找</h4><ul>
<li><h5 id="简单的二分查找，具体代码如下：（2023-09-12午）"><a href="#简单的二分查找，具体代码如下：（2023-09-12午）" class="headerlink" title="简单的二分查找，具体代码如下：（2023/09/12午）"></a>简单的二分查找，具体代码如下：（2023/09/12午）</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">   * 循环实现二分查找</span><br><span class="hljs-comment">   *</span><br><span class="hljs-comment">   * <span class="hljs-doctag">@param</span> array</span><br><span class="hljs-comment">   * <span class="hljs-doctag">@param</span> low</span><br><span class="hljs-comment">   * <span class="hljs-doctag">@param</span> high</span><br><span class="hljs-comment">   * <span class="hljs-doctag">@param</span> target</span><br><span class="hljs-comment">   * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">   */</span><br>  <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-type">int</span> <span class="hljs-title function_">binarySearch1</span><span class="hljs-params">(<span class="hljs-type">int</span>[] array, <span class="hljs-type">int</span> low, <span class="hljs-type">int</span> high, <span class="hljs-type">int</span> target)</span> &#123;<br><br>      <span class="hljs-comment">// 循环</span><br>      <span class="hljs-keyword">while</span> (low &lt;= high) &#123;<br>          <span class="hljs-type">int</span> <span class="hljs-variable">mid</span> <span class="hljs-operator">=</span> (low + high) / <span class="hljs-number">2</span>;<br>	<span class="hljs-comment">//1.右移提高性能</span><br>          <span class="hljs-keyword">if</span> (array[mid] == target) &#123;<br>              <span class="hljs-keyword">return</span> mid;<br>          &#125; <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (array[mid] &gt; target) &#123;<br>              <span class="hljs-comment">// 由于array[mid]不是目标值，因此再次递归搜索时，可以将其排除</span><br>              high = mid - <span class="hljs-number">1</span>;<br>          &#125; <span class="hljs-keyword">else</span> &#123;<br>              <span class="hljs-comment">// 由于array[mid]不是目标值，因此再次递归搜索时，可以将其排除</span><br>              low = mid + <span class="hljs-number">1</span>;<br>          &#125;<br>      &#125;<br>      <span class="hljs-keyword">return</span> -<span class="hljs-number">1</span>;<br>  &#125;<br></code></pre></td></tr></table></figure>

<ul>
<li>待优化有两点：<strong>解决溢出问题</strong>、<strong>除法-&gt;移位（提高运算效率）</strong></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">   * 循环实现二分查找</span><br><span class="hljs-comment">   *</span><br><span class="hljs-comment">   * <span class="hljs-doctag">@param</span> array</span><br><span class="hljs-comment">   * <span class="hljs-doctag">@param</span> low</span><br><span class="hljs-comment">   * <span class="hljs-doctag">@param</span> high</span><br><span class="hljs-comment">   * <span class="hljs-doctag">@param</span> target</span><br><span class="hljs-comment">   * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">   */</span><br>  <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-type">int</span> <span class="hljs-title function_">binarySearch1</span><span class="hljs-params">(<span class="hljs-type">int</span>[] array, <span class="hljs-type">int</span> low, <span class="hljs-type">int</span> high, <span class="hljs-type">int</span> target)</span> &#123;<br><br>      <span class="hljs-comment">// 循环</span><br>      <span class="hljs-keyword">while</span> (low &lt;= high) &#123;<br>          <span class="hljs-type">int</span> <span class="hljs-variable">mid</span> <span class="hljs-operator">=</span> low + ((high - low) &gt;&gt; <span class="hljs-number">1</span>); <span class="hljs-comment">// 防止溢出 右移提高性能</span><br>          <span class="hljs-keyword">if</span> (array[mid] == target) &#123;<br>              <span class="hljs-keyword">return</span> mid;<br>          &#125; <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (array[mid] &gt; target) &#123;<br>              <span class="hljs-comment">// 由于array[mid]不是目标值，因此再次递归搜索时，可以将其排除</span><br>              high = mid - <span class="hljs-number">1</span>;<br>          &#125; <span class="hljs-keyword">else</span> &#123;<br>              <span class="hljs-comment">// 由于array[mid]不是目标值，因此再次递归搜索时，可以将其排除</span><br>              low = mid + <span class="hljs-number">1</span>;<br>          &#125;<br>      &#125;<br>      <span class="hljs-keyword">return</span> -<span class="hljs-number">1</span>;<br>  &#125;<br></code></pre></td></tr></table></figure>

<ul>
<li>上面提到的二分查找是最常用的<strong>循环法</strong>，还有一种<strong>递归法：</strong>（<strong>2023/09/12午</strong>）</li>
</ul>
<h4 id="二分查找（递归法）"><a href="#二分查找（递归法）" class="headerlink" title="二分查找（递归法）"></a>二分查找（递归法）</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">    * 方法二：递归方式实现</span><br><span class="hljs-comment">    *</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@param</span> array</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@param</span> low</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@param</span> high</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@param</span> target</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">    */</span><br>   <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-type">int</span> <span class="hljs-title function_">binarySearch2</span><span class="hljs-params">(<span class="hljs-type">int</span>[] array, <span class="hljs-type">int</span> low, <span class="hljs-type">int</span> high, <span class="hljs-type">int</span> target)</span> &#123;<br>       <span class="hljs-comment">//递归终止条件</span><br>       <span class="hljs-keyword">if</span> (low &lt; high) &#123;<br>           <span class="hljs-type">int</span> mid=low + ((high-low) &gt;&gt; <span class="hljs-number">1</span>);<br>           <span class="hljs-keyword">if</span> (array[mid] == target) &#123;<br>               <span class="hljs-keyword">return</span> mid;  <span class="hljs-comment">// 返回目标值的位置，从1开始</span><br>           &#125;<br>           <span class="hljs-keyword">if</span> (array[mid] &gt; target) &#123;<br>               <span class="hljs-comment">// 由于array[mid]不是目标值，因此再次递归搜索时，可以将其排除</span><br>               <span class="hljs-keyword">return</span> binarySearch2(array, low, mid - <span class="hljs-number">1</span>, target);<br>           &#125; <span class="hljs-keyword">else</span> &#123;<br>               <span class="hljs-comment">// 由于array[mid]不是目标值，因此再次递归搜索时，可以将其排除</span><br>               <span class="hljs-keyword">return</span> binarySearch2(array, mid + <span class="hljs-number">1</span>, high, target);<br>           &#125;<br>       &#125;<br>       <span class="hljs-keyword">return</span> -<span class="hljs-number">1</span>;   <span class="hljs-comment">//表示没有搜索到</span><br>   &#125;<br></code></pre></td></tr></table></figure>

<h4 id="含重复元素的二分查找"><a href="#含重复元素的二分查找" class="headerlink" title="含重复元素的二分查找"></a>含重复元素的二分查找</h4><ul>
<li>思路：<ul>
<li>找到指定元素 target 后，继续向左查找，mid–，</li>
<li>直到到达数组左边界，或者 nums[mid] 不等于 target 为止 </li>
<li>返回 nums[mid++]</li>
</ul>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 含重复元素的二分搜索</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> nums</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> target</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-type">int</span> <span class="hljs-title function_">search1</span><span class="hljs-params">(<span class="hljs-type">int</span>[] nums, <span class="hljs-type">int</span> target)</span> &#123;<br>        <span class="hljs-keyword">if</span> (nums == <span class="hljs-literal">null</span> || nums.length == <span class="hljs-number">0</span>)<br>            <span class="hljs-keyword">return</span> -<span class="hljs-number">1</span>;<br>        <span class="hljs-type">int</span> <span class="hljs-variable">left</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;<br><br>        <span class="hljs-keyword">if</span> (nums[<span class="hljs-number">0</span>] == target) &#123;<br>            <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>        &#125;<br><br>        <span class="hljs-type">int</span> <span class="hljs-variable">right</span> <span class="hljs-operator">=</span> nums.length - <span class="hljs-number">1</span>;<br>        <span class="hljs-keyword">while</span> (left &lt;= right) &#123;<br>            <span class="hljs-type">int</span> <span class="hljs-variable">mid</span> <span class="hljs-operator">=</span> left + (right - left) / <span class="hljs-number">2</span>;<br>            <span class="hljs-keyword">if</span> (nums[mid] &lt; target) &#123;<br>                left = mid + <span class="hljs-number">1</span>;<br>            &#125; <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (nums[mid] &gt; target) &#123;<br>                right = mid - <span class="hljs-number">1</span>;<br>            &#125; <span class="hljs-keyword">else</span> &#123;<br>                <span class="hljs-comment">//找到之后，往左边找</span><br>                <span class="hljs-keyword">while</span> (mid != <span class="hljs-number">0</span> &amp;&amp; nums[mid] == target)<br>                    mid--;<br>                <span class="hljs-keyword">return</span> mid + <span class="hljs-number">1</span>;<br>            &#125;<br>        &#125;<br>        <span class="hljs-keyword">return</span> -<span class="hljs-number">1</span>;<br>    &#125;<br></code></pre></td></tr></table></figure>

<h3 id="实战训练（白银挑战）-6"><a href="#实战训练（白银挑战）-6" class="headerlink" title="实战训练（白银挑战）"></a>实战训练（白银挑战）</h3><h4 id="山脉数组峰顶查找"><a href="#山脉数组峰顶查找" class="headerlink" title="山脉数组峰顶查找"></a>山脉数组峰顶查找</h4><ul>
<li>情景：给定一个山脉数组，查找峰顶（即最大的数）</li>
</ul>
<figure class="highlight livescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs livescript">         <span class="hljs-number">17</span>  <br>      /      <span class="hljs-string">\</span><br>    <span class="hljs-number">5</span>          <span class="hljs-number">7</span><br>  /              <span class="hljs-string">\</span><br><span class="hljs-number">1</span>                  <span class="hljs-number">3</span><br></code></pre></td></tr></table></figure>

<ul>
<li><p>如上图所示，峰顶为17，那么峰顶有什么特别之处吗？</p>
<ul>
<li><p>当然是：<strong>nums[i] &gt; nums[i - 1] &amp;&amp; nums[i] &gt; nums[i + 1]</strong></p>
</li>
<li><p>所以跟普通的二分查找相比，仅仅多考虑到了进行节点左右的大小比较</p>
</li>
</ul>
</li>
<li><h5 id="具体代码如下：（2023-09-15晚）"><a href="#具体代码如下：（2023-09-15晚）" class="headerlink" title="具体代码如下：（2023/09/15晚）"></a>具体代码如下：（2023/09/15晚）</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**  </span><br><span class="hljs-comment">     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可  </span><br><span class="hljs-comment">     *  </span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> nums int整型一维数组  </span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> int整型  </span><br><span class="hljs-comment">     */</span>  <br>    <span class="hljs-keyword">public</span> <span class="hljs-type">int</span> <span class="hljs-title function_">findPeakElement</span><span class="hljs-params">(<span class="hljs-type">int</span>[] arr)</span> &#123;  <br>        <span class="hljs-type">int</span> <span class="hljs-variable">n</span> <span class="hljs-operator">=</span> arr.length;  <br>  <br>        <span class="hljs-keyword">if</span> (n == <span class="hljs-number">1</span>) &#123;  <br>            <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>; <span class="hljs-comment">// 如果数组只有一个元素，它自然就是峰值  </span><br>        &#125;  <br>  <br>        <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i &lt; n - <span class="hljs-number">1</span>; ++i) &#123;  <br>            <span class="hljs-keyword">if</span> (arr[i] &gt; arr[i + <span class="hljs-number">1</span>]) &#123;  <br>                <span class="hljs-keyword">return</span> i; <span class="hljs-comment">// 如果当前元素大于其下一个元素，则找到了峰值，返回其索引  </span><br>            &#125;  <br>        &#125;  <br>  <br>        <span class="hljs-comment">// 如果循环结束还没有返回，说明数组是递增的，那么最后一个元素是峰值  </span><br>        <span class="hljs-keyword">return</span> n - <span class="hljs-number">1</span>;  <br>    &#125;  <br></code></pre></td></tr></table></figure>

<h4 id="旋转数组最小数字"><a href="#旋转数组最小数字" class="headerlink" title="旋转数组最小数字"></a>旋转数组最小数字</h4><figure class="highlight accesslog"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs accesslog">输入: nums = <span class="hljs-string">[4,4,4,5,1,2,3]</span><br>输出: <span class="hljs-number">1</span><br>原数组为<span class="hljs-string">[1,2,3,4,5]</span>，旋转<span class="hljs-number">3</span>次得到输入数组<br></code></pre></td></tr></table></figure>

<ul>
<li>这个图很形象：</li>
</ul>
<p><img src="https://gitee.com/deng-2022/pictures/raw/master/images/image-20230915200727172.png" alt="image-20230915200727172"></p>
<ul>
<li><h5 id="最小值就在中轴线，仍然是考虑到了进行节点左右大小的比较，并且在二分结束之后，直接拿到-nums-low-，即为最小值"><a href="#最小值就在中轴线，仍然是考虑到了进行节点左右大小的比较，并且在二分结束之后，直接拿到-nums-low-，即为最小值" class="headerlink" title="最小值就在中轴线，仍然是考虑到了进行节点左右大小的比较，并且在二分结束之后，直接拿到 nums[low]，即为最小值"></a>最小值就在中轴线，仍然是考虑到了进行节点左右大小的比较，并且在二分结束之后，直接拿到 nums[low]，即为最小值</h5></li>
<li><h5 id="具体代码如下：-9"><a href="#具体代码如下：-9" class="headerlink" title="具体代码如下："></a>具体代码如下：</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 旋转数字的最小数字</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> nums</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-type">int</span> <span class="hljs-title function_">findMin</span><span class="hljs-params">(<span class="hljs-type">int</span>[] nums)</span> &#123;<br>        <span class="hljs-type">int</span> <span class="hljs-variable">low</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;<br>        <span class="hljs-type">int</span> <span class="hljs-variable">high</span> <span class="hljs-operator">=</span> nums.length - <span class="hljs-number">1</span>;<br>        <span class="hljs-keyword">while</span> (low &lt; high) &#123;<br>            <span class="hljs-type">int</span> <span class="hljs-variable">pivot</span> <span class="hljs-operator">=</span> low + ((high - low) &gt;&gt; <span class="hljs-number">1</span>);<br>            <span class="hljs-keyword">if</span> (nums[pivot] &lt; nums[high]) &#123;<br>                high = pivot;<br>            &#125; <span class="hljs-keyword">else</span> &#123;<br>                low = pivot + <span class="hljs-number">1</span>;<br>            &#125;<br>        &#125;<br>        <span class="hljs-keyword">return</span> nums[low];<br>    &#125;<br></code></pre></td></tr></table></figure>

<h4 id="有序数组缺失数字"><a href="#有序数组缺失数字" class="headerlink" title="有序数组缺失数字"></a>有序数组缺失数字</h4><ul>
<li><h5 id="简单，二分查找，当-nums-i-i-时，就拿到了这个数字（2023-09-15晚）"><a href="#简单，二分查找，当-nums-i-i-时，就拿到了这个数字（2023-09-15晚）" class="headerlink" title="简单，二分查找，当 nums[i] != i 时，就拿到了这个数字（2023/09/15晚）"></a>简单，二分查找，当 nums[i] != i 时，就拿到了这个数字（2023/09/15晚）</h5></li>
<li><h5 id="具体代码如下：-10"><a href="#具体代码如下：-10" class="headerlink" title="具体代码如下："></a>具体代码如下：</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">    * 缺失数字</span><br><span class="hljs-comment">    *</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@param</span> a</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">    */</span><br>   <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-type">int</span> <span class="hljs-title function_">solve</span><span class="hljs-params">(<span class="hljs-type">int</span>[] a)</span> &#123;<br>       <span class="hljs-comment">// write code here</span><br>       <span class="hljs-type">int</span> <span class="hljs-variable">left</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;<br>       <span class="hljs-type">int</span> <span class="hljs-variable">right</span> <span class="hljs-operator">=</span> a.length - <span class="hljs-number">1</span>;<br>       <span class="hljs-keyword">while</span> (left &lt; right) &#123;<br>           <span class="hljs-type">int</span> <span class="hljs-variable">mid</span> <span class="hljs-operator">=</span> (left + right) / <span class="hljs-number">2</span>;<br>           <span class="hljs-keyword">if</span> (a[mid] == mid) &#123;<br>               left = mid + <span class="hljs-number">1</span>;<br>           &#125; <span class="hljs-keyword">else</span> &#123;<br>               right = mid;<br>           &#125;<br>       &#125;<br>       <span class="hljs-keyword">return</span> left;<br>   &#125;<br></code></pre></td></tr></table></figure>

<h4 id="优化求平方根"><a href="#优化求平方根" class="headerlink" title="优化求平方根"></a>优化求平方根</h4><h2 id="快速排序"><a href="#快速排序" class="headerlink" title="快速排序"></a>快速排序</h2><h3 id="基础知识（青铜挑战）-7"><a href="#基础知识（青铜挑战）-7" class="headerlink" title="基础知识（青铜挑战）"></a>基础知识（青铜挑战）</h3><ul>
<li><p>快速排序通过多次比较与交换来完成排序。而这个过程又被分为了多次重复单趟排序，接下来我们先从每一趟的排序讲起。</p>
</li>
<li><p>快速排序思想很简单，也很重要：</p>
</li>
<li><p>在一个无序数组中取一个数key，每一趟排序的最终目的是：让key的左边的所有数小于key，key的右边都大于key（假设排升序）</p>
</li>
<li><p> 接下来，我们对key的左右区间进行单趟排序，可以预见的是，每次排序都固定好了一个数。而当我们对细分的左右区间进行单趟排序，最终整个数组都会化为有序</p>
</li>
<li><p>最常用的快排实现方法：<strong>快慢指针法</strong></p>
<ul>
<li>取最左边的数为key</li>
<li>定义两个快慢指针，都从key的下一位往右走</li>
<li>fast每走一步判断一下它指向的元素是否小于key，若小于则交换fast和slow位置的元素，并且让slow向前走</li>
<li>直到fast走到底，结束循环</li>
<li>最后让slow和key位置的值交换，再返回key的位置</li>
</ul>
</li>
<li><h5 id="具体代码如下：（2023-09-19午）"><a href="#具体代码如下：（2023-09-19午）" class="headerlink" title="具体代码如下：（2023/09/19午）"></a>具体代码如下：（2023/09/19午）</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">quickSort</span><span class="hljs-params">(<span class="hljs-type">int</span>[] nums, <span class="hljs-type">int</span> start, <span class="hljs-type">int</span> end)</span> &#123;<br>        <span class="hljs-comment">// 判断递归结束的条件</span><br>        <span class="hljs-keyword">if</span> (start &gt;= end) &#123;<br>            <span class="hljs-keyword">return</span>;<br>        &#125;<br>        <span class="hljs-comment">// 选定最左边的元素作为基准值</span><br>        <span class="hljs-type">int</span> <span class="hljs-variable">pivot</span> <span class="hljs-operator">=</span> nums[start];<br>        <span class="hljs-comment">// 初始化快慢指针</span><br>        <span class="hljs-type">int</span> <span class="hljs-variable">slow</span> <span class="hljs-operator">=</span> start;<br>        <span class="hljs-type">int</span> <span class="hljs-variable">fast</span> <span class="hljs-operator">=</span> start + <span class="hljs-number">1</span>;<br><br>        <span class="hljs-comment">// 快慢指针法进行划分</span><br>        <span class="hljs-keyword">while</span> (fast &lt;= end) &#123;<br>            <span class="hljs-keyword">if</span> (nums[fast] &lt; pivot) &#123;<br>                <span class="hljs-comment">// 如果当前元素小于基准值，将其交换到基准值的左侧</span><br>                slow++;<br>                swap(nums, slow, fast);<br>            &#125;<br>            fast++;<br>        &#125;<br>        <span class="hljs-comment">// 将基准值交换到正确的位置</span><br>        swap(nums, start, slow);<br><br>        <span class="hljs-comment">// 递归对基准值的左右两部分进行排序</span><br>        quickSort(nums, start, slow - <span class="hljs-number">1</span>);<br>        quickSort(nums, slow + <span class="hljs-number">1</span>, end);<br>    &#125;<br><br>    <span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">swap</span><span class="hljs-params">(<span class="hljs-type">int</span>[] nums, <span class="hljs-type">int</span> i, <span class="hljs-type">int</span> j)</span> &#123;<br>        <span class="hljs-type">int</span> <span class="hljs-variable">temp</span> <span class="hljs-operator">=</span> nums[i];<br>        nums[i] = nums[j];<br>        nums[j] = temp;<br>    &#125;<br></code></pre></td></tr></table></figure>

<h3 id="实战训练（白银挑战）-7"><a href="#实战训练（白银挑战）-7" class="headerlink" title="实战训练（白银挑战）"></a>实战训练（白银挑战）</h3><h4 id="数组中K大的数"><a href="#数组中K大的数" class="headerlink" title="数组中K大的数"></a>数组中K大的数</h4><ul>
<li><h5 id="我们要找第K大的元素，如果我们能够将数组排好序，再取-nums-K-1-元素，就是目标值了"><a href="#我们要找第K大的元素，如果我们能够将数组排好序，再取-nums-K-1-元素，就是目标值了" class="headerlink" title="我们要找第K大的元素，如果我们能够将数组排好序，再取 nums[K - 1] 元素，就是目标值了"></a>我们要找第K大的元素，如果我们能够将数组排好序，再取 nums[K - 1] 元素，就是目标值了</h5></li>
<li><p>试想一下，每一轮快排结束之后，我们能知晓基准值的位置，即我们知晓基准值的下标</p>
<ul>
<li><p>如果目标元素在基准元素的左侧，就对左侧进行递归快排</p>
</li>
<li><p>否则，对右侧进行递归快排</p>
</li>
<li><p>最后，在已经 ”排好序的“ 数组中，轻松就能找到第K大的元素</p>
</li>
<li><h5 id="其实就是快速排序对半砍，每次只快排一半元素，要么左侧，要么右侧（2023-09-27晚）"><a href="#其实就是快速排序对半砍，每次只快排一半元素，要么左侧，要么右侧（2023-09-27晚）" class="headerlink" title="其实就是快速排序对半砍，每次只快排一半元素，要么左侧，要么右侧（2023/09/27晚）"></a>其实就是快速排序对半砍，每次只快排一半元素，要么左侧，要么右侧（2023/09/27晚）</h5></li>
</ul>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">quicksort</span><span class="hljs-params">(<span class="hljs-type">int</span>[] nums, <span class="hljs-type">int</span> left, <span class="hljs-type">int</span> right)</span> &#123;<br>       <span class="hljs-type">int</span> <span class="hljs-variable">start</span> <span class="hljs-operator">=</span> left;<br>       <span class="hljs-type">int</span> <span class="hljs-variable">end</span> <span class="hljs-operator">=</span> right;<br>       <span class="hljs-type">int</span> <span class="hljs-variable">pivot</span> <span class="hljs-operator">=</span> nums[(end + start) / <span class="hljs-number">2</span>]; <span class="hljs-comment">//取中位值作左右参考对比</span><br><br>       <span class="hljs-comment">//每次循环都将大的放到左边，小的放到右边</span><br>       <span class="hljs-keyword">while</span> (start &lt; end) &#123;<br>           <span class="hljs-keyword">while</span> (nums[start] &gt; pivot) &#123;<br>               start++;<br>           &#125;<br><br>           <span class="hljs-keyword">while</span> (nums[end] &lt; pivot) &#123;<br>               end--;<br>           &#125;<br><br>           <span class="hljs-comment">//如果l&gt;=r说明mid左边的值全部大于等于mid的值，右边全是小的，退出</span><br>           <span class="hljs-keyword">if</span> (start &gt;= end) &#123;<br>               <span class="hljs-keyword">break</span>;<br>           &#125;<br>           <span class="hljs-comment">//交换</span><br>           <span class="hljs-type">int</span> <span class="hljs-variable">temp</span> <span class="hljs-operator">=</span> nums[start];<br>           nums[start] = nums[end];<br>           nums[end] = temp;<br><br>           <span class="hljs-comment">//解决值相同的情况，如果交换后发现左边的值等于mid目标值，则使右边指针向左移</span><br>           <span class="hljs-keyword">if</span> (nums[start] == pivot) &#123;<br>               end--;<br>           &#125;<br>           <span class="hljs-comment">//右边的值等于mid目标值，则使左边指针向左移</span><br>           <span class="hljs-keyword">if</span> (nums[end] == pivot) &#123;<br>               start++;<br>           &#125;<br>       &#125;<br><br>       <span class="hljs-keyword">if</span> (start == end) &#123; <span class="hljs-comment">//退出循环防止栈溢出</span><br>           start++;<br>           end--;<br>       &#125;<br>       <br>       <span class="hljs-keyword">if</span> (left &lt; end) &#123;<br>           quicksort(nums, left, end);   <span class="hljs-comment">//向左递归</span><br>       &#125;<br>       <br>       <span class="hljs-keyword">if</span> (right &gt; start) &#123;<br>           quicksort(nums, start, right); <span class="hljs-comment">//向右递归</span><br>       &#125;<br>   &#125;<br></code></pre></td></tr></table></figure>

<h4 id="归并排序"><a href="#归并排序" class="headerlink" title="归并排序"></a>归并排序</h4><ul>
<li>好难好难，需要先理解思路，再考虑编码实现：<ul>
<li>归并排序简单来讲，就是将大的序列视为若干小的数组，利用归并思想实现排序的方法。</li>
<li>分治策略（分：把问题分成小的问题分别求解；治：将分的阶段得到的各答案合在一起）</li>
</ul>
</li>
</ul>
<h2 id="位运算"><a href="#位运算" class="headerlink" title="位运算"></a>位运算</h2><h3 id="基础知识（青铜排序）"><a href="#基础知识（青铜排序）" class="headerlink" title="基础知识（青铜排序）"></a>基础知识（青铜排序）</h3><ul>
<li>原码、反码、补码</li>
<li>与运算 &amp;，或运算 |，异或运算 ^，取反运算 !</li>
<li>移位运算：左移、算术右移、逻辑右移</li>
<li>移位运算与乘除法的关系</li>
</ul>
<h2 id="字符串"><a href="#字符串" class="headerlink" title="字符串"></a>字符串</h2><h3 id="基础知识（青铜挑战）-8"><a href="#基础知识（青铜挑战）-8" class="headerlink" title="基础知识（青铜挑战）"></a>基础知识（青铜挑战）</h3><h4 id="转换成小写字母"><a href="#转换成小写字母" class="headerlink" title="转换成小写字母"></a>转换成小写字母</h4><ul>
<li><p>遍历该字符串，依次转换为小写，存入新的字符串中</p>
</li>
<li><h5 id="具体代码如下：-11"><a href="#具体代码如下：-11" class="headerlink" title="具体代码如下："></a>具体代码如下：</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> String <span class="hljs-title function_">toLowerCase</span><span class="hljs-params">(String s)</span> &#123;<br>      <span class="hljs-type">int</span> <span class="hljs-variable">n</span> <span class="hljs-operator">=</span> s.length();<br>      <span class="hljs-type">char</span>[] chars = s.toCharArray();<br>      <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i &lt; n; ++i) &#123;<br>          <span class="hljs-keyword">if</span> (chars[i] &gt;= <span class="hljs-number">65</span> &amp;&amp; chars[i] &lt;= <span class="hljs-number">90</span>) &#123;<br>              chars[i] += <span class="hljs-number">32</span>;<br>          &#125;<br>      &#125;<br>      <span class="hljs-type">String</span> <span class="hljs-variable">str</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">String</span>(chars);<br>      <span class="hljs-keyword">return</span> str;<br>  &#125;<br></code></pre></td></tr></table></figure>

<h3 id="实战训练（白银挑战）-8"><a href="#实战训练（白银挑战）-8" class="headerlink" title="实战训练（白银挑战）"></a>实战训练（白银挑战）</h3><h4 id="反转字符串"><a href="#反转字符串" class="headerlink" title="反转字符串"></a>反转字符串</h4><ul>
<li><p>思路：</p>
<ul>
<li>使用双指针，一个在前，一个在后，相向移动，遍历字符串</li>
<li>在遍历的过程中，将两指针指向的元素交换位置</li>
<li>直到两指针相遇</li>
</ul>
</li>
<li><h5 id="具体代码如下：-2023-09-30早"><a href="#具体代码如下：-2023-09-30早" class="headerlink" title="具体代码如下：(2023/09/30早)"></a>具体代码如下：(2023/09/30早)</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">reverseString</span><span class="hljs-params">(<span class="hljs-type">char</span>[] s)</span> &#123;<br>       <span class="hljs-type">int</span> <span class="hljs-variable">n</span> <span class="hljs-operator">=</span> s.length;<br>       <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">left</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>, right = n - <span class="hljs-number">1</span>; left &lt; right; ++left, --right) &#123;<br>           <span class="hljs-type">char</span> <span class="hljs-variable">tmp</span> <span class="hljs-operator">=</span> s[left];<br>           s[left] = s[right];<br>           s[right] = tmp;<br>       &#125;<br>   &#125;<br></code></pre></td></tr></table></figure>

<h4 id="K个一组反转"><a href="#K个一组反转" class="headerlink" title="K个一组反转"></a>K个一组反转</h4><ul>
<li></li>
</ul>
<h4 id="仅反转字母"><a href="#仅反转字母" class="headerlink" title="仅反转字母"></a>仅反转字母</h4><ul>
<li><p>我们有两种思路：</p>
<ul>
<li>将所有的字母压栈</li>
<li>双指针遍历字符串</li>
</ul>
</li>
<li><h5 id="压栈法："><a href="#压栈法：" class="headerlink" title="压栈法："></a>压栈法：</h5><ul>
<li>遍整个字符串，将字母依次压入栈中</li>
<li>创建一个新的字符串对象，再次遍历该字符串：如果是非字母，将该字符放入新字符串；如果是字母，则从存入弹出的栈顶元素</li>
<li>直到遍历结束</li>
</ul>
</li>
<li><h5 id="具体代码如下：-12"><a href="#具体代码如下：-12" class="headerlink" title="具体代码如下："></a>具体代码如下：</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> String <span class="hljs-title function_">reverseOnlyLetters</span><span class="hljs-params">(String S)</span> &#123;<br>       Stack&lt;Character&gt; letters = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Stack</span>();<br>       <span class="hljs-keyword">for</span> (<span class="hljs-type">char</span> c : S.toCharArray())<br>           <span class="hljs-keyword">if</span> (Character.isLetter(c))<br>               letters.push(c);<br><br>       <span class="hljs-type">StringBuilder</span> <span class="hljs-variable">ans</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">StringBuilder</span>();<br>       <span class="hljs-keyword">for</span> (<span class="hljs-type">char</span> c : S.toCharArray()) &#123;<br>           <span class="hljs-keyword">if</span> (Character.isLetter(c))<br>               ans.append(letters.pop());<br>           <span class="hljs-keyword">else</span><br>               ans.append(c);<br>       &#125;<br><br>       <span class="hljs-keyword">return</span> ans.toString();<br>   &#125;<br></code></pre></td></tr></table></figure>

<ul>
<li><h5 id="双指针法："><a href="#双指针法：" class="headerlink" title="双指针法："></a>双指针法：</h5><ul>
<li>定义两个指针，一个从头向后遍历，一个从后向前，维护字母字符</li>
<li>创建一个新的字符串对象，前指针遍历字符串</li>
<li>如果是非字母，则将该字符放入新字符串；如果是字母，则移动后指针，找到字母，将该字母存入新的字符串</li>
<li>直到前指针遍历结束</li>
</ul>
</li>
<li><h5 id="具体代码如下：-13"><a href="#具体代码如下：-13" class="headerlink" title="具体代码如下："></a>具体代码如下：</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> String <span class="hljs-title function_">reverseOnlyLetters2</span><span class="hljs-params">(String S)</span> &#123;<br>     <span class="hljs-keyword">if</span> (S == <span class="hljs-literal">null</span> || S.length() == <span class="hljs-number">0</span>)&#123;<br>         <span class="hljs-keyword">return</span>  S;<br>     &#125;<br><br>     <span class="hljs-type">StringBuffer</span> <span class="hljs-variable">ans</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">StringBuffer</span>();<br>     <span class="hljs-type">int</span> <span class="hljs-variable">j</span> <span class="hljs-operator">=</span>  S.length() - <span class="hljs-number">1</span>;<br>     <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i &lt; S.length(); i++) &#123;<br>         <span class="hljs-keyword">if</span>(Character.isLetter(S.charAt(i)))&#123;<br>             <span class="hljs-keyword">while</span> (!Character.isLetter(S.charAt(j)))<br>                 j--;<br>             ans.append(S.charAt(j--));<br>         &#125;<span class="hljs-keyword">else</span> &#123;<br>             ans.append(S.charAt(i));<br>         &#125;<br>     &#125;<br>     <span class="hljs-keyword">return</span> ans.toString();<br> &#125;<br></code></pre></td></tr></table></figure>

<h4 id="反转字符串里的单词"><a href="#反转字符串里的单词" class="headerlink" title="反转字符串里的单词"></a>反转字符串里的单词</h4><ul>
<li><p>可以使用语言提供的方法解决：</p>
<ul>
<li>将字符串按空格分开，转换为数组/集合</li>
<li>使用 reserve 方法，反转数组/集合元素</li>
<li>再将数组/集合转换为字符串</li>
</ul>
</li>
<li><h5 id="具体代码如下：-2023-09-30早-1"><a href="#具体代码如下：-2023-09-30早-1" class="headerlink" title="具体代码如下：(2023/09/30早)"></a>具体代码如下：(2023/09/30早)</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> String <span class="hljs-title function_">reverseWords2</span><span class="hljs-params">(String s)</span> &#123;<br>      <span class="hljs-keyword">if</span> (s == <span class="hljs-literal">null</span> || s.length() == <span class="hljs-number">0</span>) &#123;<br>          <span class="hljs-keyword">return</span> s;<br>      &#125;<br>      <span class="hljs-comment">// 取出首尾空白字符串</span><br>      s = s.trim();<br>      <span class="hljs-comment">// 分割单词</span><br>      String[] split = s.split(<span class="hljs-string">&quot;\\s+&quot;</span>);<br>      List&lt;String&gt; wordList = Arrays.asList(split);<br>      <span class="hljs-comment">// 反转</span><br>      Collections.reverse(wordList);<br>      <span class="hljs-comment">// 转换为字符串</span><br>      <span class="hljs-keyword">return</span> String.join(<span class="hljs-string">&quot; &quot;</span>, wordList);<br>  &#125;<br></code></pre></td></tr></table></figure>

<h4 id="验证回文串"><a href="#验证回文串" class="headerlink" title="验证回文串"></a>验证回文串</h4><ul>
<li>什么是回文字符串？示例如下：</li>
</ul>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs css"><span class="hljs-selector-tag">A</span> man, <span class="hljs-selector-tag">a</span> plan, <span class="hljs-selector-tag">a</span> canal: Panama<br>将该字符串反转后: amanaplanacanalpanama，这就是回文字符串<br></code></pre></td></tr></table></figure>

<ul>
<li><p>我们仍然使用双指针法解决：</p>
<ul>
<li>首先要做的就是遍历一遍字符串，把所有的字母都放到新的字符串中</li>
<li>再使用双指针相向遍历新字符串，遍历的的过程中，判断两指针指向元素是否相同</li>
<li>直到两指针相遇，说明是回文字符串</li>
</ul>
</li>
<li><h5 id="具体代码如下：-14"><a href="#具体代码如下：-14" class="headerlink" title="具体代码如下："></a>具体代码如下：</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-type">boolean</span> <span class="hljs-title function_">isPalindrome</span><span class="hljs-params">(String s)</span> &#123;<br>       <span class="hljs-type">StringBuffer</span> <span class="hljs-variable">sgood</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">StringBuffer</span>();<br>       <span class="hljs-type">int</span> <span class="hljs-variable">length</span> <span class="hljs-operator">=</span> s.length();<br>       <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i &lt; length; i++) &#123;<br>           <span class="hljs-type">char</span> <span class="hljs-variable">ch</span> <span class="hljs-operator">=</span> s.charAt(i);<br>           <span class="hljs-keyword">if</span> (Character.isLetterOrDigit(ch)) &#123;<br>               sgood.append(Character.toLowerCase(ch));<br>           &#125;<br>       &#125;<br>       <span class="hljs-type">StringBuffer</span> <span class="hljs-variable">sgood_rev</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">StringBuffer</span>(sgood).reverse();<br>       <span class="hljs-keyword">return</span> sgood.toString().equals(sgood_rev.toString());<br>   &#125;<br></code></pre></td></tr></table></figure>

<h4 id="字符串里的第一个唯一字符"><a href="#字符串里的第一个唯一字符" class="headerlink" title="字符串里的第一个唯一字符"></a>字符串里的第一个唯一字符</h4><ul>
<li><p>最好用的方法：Hash法</p>
<ul>
<li>定义一个 Map 集合，我们遍历一遍字符串，使用 Map 记录每个字符出现过的次数</li>
<li>再次遍历该 Map，取到第一个出现次数大于1的元素</li>
</ul>
</li>
<li><h5 id="具体代码如下：-2023-09-30早-2"><a href="#具体代码如下：-2023-09-30早-2" class="headerlink" title="具体代码如下：(2023/09/30早)"></a>具体代码如下：(2023/09/30早)</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-type">int</span> <span class="hljs-title function_">firstUniqChar</span><span class="hljs-params">(String s)</span> &#123;<br>     <span class="hljs-keyword">if</span> (s == <span class="hljs-literal">null</span> || s.length() == <span class="hljs-number">0</span>) &#123;<br>         <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>     &#125;<br>     Map&lt;Character, Integer&gt; frequency = <span class="hljs-keyword">new</span> <span class="hljs-title class_">HashMap</span>&lt;Character, Integer&gt;();<br>     <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i &lt; s.length(); ++i) &#123;<br>         <span class="hljs-type">char</span> <span class="hljs-variable">ch</span> <span class="hljs-operator">=</span> s.charAt(i);<br>         frequency.put(ch, frequency.getOrDefault(ch, <span class="hljs-number">0</span>) + <span class="hljs-number">1</span>);<br>     &#125;<br>     <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i &lt; s.length(); ++i) &#123;<br>         <span class="hljs-keyword">if</span> (frequency.get(s.charAt(i)) == <span class="hljs-number">1</span>) &#123;<br>             <span class="hljs-keyword">return</span> i;<br>         &#125;<br>     &#125;<br>     <span class="hljs-keyword">return</span> -<span class="hljs-number">1</span>;<br> &#125;<br></code></pre></td></tr></table></figure>

<h4 id="判断是否互为字符重排"><a href="#判断是否互为字符重排" class="headerlink" title="判断是否互为字符重排"></a>判断是否互为字符重排</h4><ul>
<li><p>什么是字符重排？就是将其中一个字符串中的字符顺序打乱重新排列后，能否成为另一个字符串</p>
</li>
<li><p>这个就更好办了，我们有两种思路：</p>
<ul>
<li>排序法</li>
<li>Map法</li>
</ul>
</li>
<li><p>排序法：</p>
<ul>
<li>分别快速地对两个字符串中的字符进行排序</li>
<li>然后直接比较两字符串是否相等即可</li>
</ul>
</li>
<li><h5 id="具体代码如下：-15"><a href="#具体代码如下：-15" class="headerlink" title="具体代码如下："></a>具体代码如下：</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-type">boolean</span> <span class="hljs-title function_">checkPermutation</span><span class="hljs-params">(String s1, String s2)</span> &#123;<br>      <span class="hljs-comment">// 将字符串转换成字符数组</span><br>      <span class="hljs-type">char</span>[] s1Chars = s1.toCharArray();<br>      <span class="hljs-type">char</span>[] s2Chars = s2.toCharArray();<br>      <span class="hljs-comment">// 对字符数组进行排序</span><br>      Arrays.sort(s1Chars);<br>      Arrays.sort(s2Chars);<br>      <span class="hljs-comment">// 再将字符数组转换成字符串，比较是否相等</span><br>      <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">String</span>(s1Chars).equals(<span class="hljs-keyword">new</span> <span class="hljs-title class_">String</span>(s2Chars));<br>  &#125;<br></code></pre></td></tr></table></figure>

<ul>
<li><p>Map法：</p>
<ul>
<li>分别统计两个字符串中，各个字符出现的次数</li>
<li>再次比较字符出现次数，只有每个字符出现的次数都一样，才能算两字符串互为字符重排</li>
</ul>
</li>
<li><h5 id="具体代码如下：-2023-09-30早-3"><a href="#具体代码如下：-2023-09-30早-3" class="headerlink" title="具体代码如下：(2023/09/30早)"></a>具体代码如下：(2023/09/30早)</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-type">boolean</span> <span class="hljs-title function_">checkPermutation2</span><span class="hljs-params">(String s1, String s2)</span> &#123;<br>        <span class="hljs-keyword">if</span> (s1.length() != s2.length()) &#123;<br>            <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;<br>        &#125;<br><br>        <span class="hljs-type">char</span>[] s1chars = s1.toCharArray();<br>        HashMap&lt;Character, Integer&gt; s1Map = getMap(s1);<br>        HashMap&lt;Character, Integer&gt; s2Map = getMap(s2);<br>        <span class="hljs-keyword">for</span> (<span class="hljs-type">char</span> s1char : s1chars) &#123;<br>            <span class="hljs-keyword">if</span> (!s2Map.containsKey(s1char) || (<span class="hljs-type">int</span>) s1Map.get(s1char) != (<span class="hljs-type">int</span>) s2Map.get(s1char)) &#123;<br>                <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;<br>            &#125;<br>        &#125;<br>        <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;<br>    &#125;<br><br>    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> HashMap&lt;Character, Integer&gt; <span class="hljs-title function_">getMap</span><span class="hljs-params">(String str)</span> &#123;<br>        HashMap&lt;Character, Integer&gt; map = <span class="hljs-keyword">new</span> <span class="hljs-title class_">HashMap</span>&lt;&gt;();<br>        <span class="hljs-type">char</span>[] chars = str.toCharArray();<br>        <span class="hljs-keyword">for</span> (<span class="hljs-type">char</span> aChar : chars) &#123;<br>            map.put(aChar, map.getOrDefault(aChar, <span class="hljs-number">0</span>) + <span class="hljs-number">1</span>);<br>        &#125;<br>        <span class="hljs-keyword">return</span> map;<br>    &#125;<br></code></pre></td></tr></table></figure>

<h2 id="堆结构"><a href="#堆结构" class="headerlink" title="堆结构"></a>堆结构</h2><h3 id="基础知识（青铜挑战）-9"><a href="#基础知识（青铜挑战）-9" class="headerlink" title="基础知识（青铜挑战）"></a>基础知识（青铜挑战）</h3><ul>
<li><p><strong>堆的概念</strong>：堆是一种数据结构，按照完全二叉树的存储顺序，将数据存储在一个一维数组中</p>
<ul>
<li><strong>大顶堆</strong>：任意节点值均大于它的左右节点值   </li>
<li><strong>小顶堆</strong>：任意节点值均大于它的左右节点值</li>
</ul>
</li>
<li><p><strong>堆的构造过程</strong>：按照层次将所有元素一次添入二叉树中，再不断调整，最终使其符合堆结构</p>
</li>
<li><p><strong>堆中插入元素</strong>：确认插入位置能够保持原二叉树为完全二叉树，再自底向上调整，保证每一层的子树都符合堆结构</p>
</li>
<li><p><strong>堆中删除元素</strong>：一般都是删除堆顶元素，将堆顶元素与二叉树最后一个元素对调，删除堆顶元素后，再依次调整每一层的各子树</p>
</li>
</ul>
<h3 id="实战训练（白银挑战）-9"><a href="#实战训练（白银挑战）-9" class="headerlink" title="实战训练（白银挑战）"></a>实战训练（白银挑战）</h3><h4 id="数组中查找第K大的元素"><a href="#数组中查找第K大的元素" class="headerlink" title="数组中查找第K大的元素"></a>数组中查找第K大的元素</h4><ul>
<li><p>这个问题<strong>很重要很经典</strong>，<strong>解决方法也有多种</strong>，我们提三个：<strong>选择法、快排、堆排序</strong></p>
</li>
<li><h5 id="堆排序："><a href="#堆排序：" class="headerlink" title="堆排序："></a>堆排序：</h5><ul>
<li>我们需要在数组中<strong>查找第K大的元素</strong>，就<strong>创建一个大小为K的小顶堆</strong></li>
<li>堆满后，只有比堆顶元素小的元素，才可以插入堆中；新插入的元素先覆盖堆顶元素后，再调整</li>
<li>将数组序列依次插入堆中，每插入一个元素，就调整堆使之符合堆的结构</li>
<li>全部序列入堆完毕后，<strong>堆顶元素就是要查找的第K大的元素</strong>了</li>
</ul>
</li>
<li><h5 id="具体代码如下：-16"><a href="#具体代码如下：-16" class="headerlink" title="具体代码如下："></a>具体代码如下：</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-type">int</span> <span class="hljs-title function_">findKthLargest</span><span class="hljs-params">(<span class="hljs-type">int</span>[] nums, <span class="hljs-type">int</span> k)</span> &#123;<br>        <span class="hljs-keyword">if</span> (k &gt; nums.length) &#123;<br>            <span class="hljs-keyword">return</span> -<span class="hljs-number">1</span>;<br>        &#125;<br>        <span class="hljs-type">int</span> <span class="hljs-variable">len</span> <span class="hljs-operator">=</span> nums.length;<br>        <span class="hljs-comment">// 使用一个含有 k 个元素的最小堆</span><br>        PriorityQueue&lt;Integer&gt; minHeap = <span class="hljs-keyword">new</span> <span class="hljs-title class_">PriorityQueue</span>&lt;&gt;(k, (a, b) -&gt; a - b);<br>        <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i &lt; k; i++) &#123;<br>            minHeap.add(nums[i]);<br>        &#125;<br>        <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> k; i &lt; len; i++) &#123;<br>            <span class="hljs-comment">// 看一眼，不拿出，因为有可能没有必要替换</span><br>            <span class="hljs-type">Integer</span> <span class="hljs-variable">topEle</span> <span class="hljs-operator">=</span> minHeap.peek();<br>            <span class="hljs-comment">// 只要当前遍历的元素比堆顶元素大，堆顶弹出，遍历的元素进去</span><br>            <span class="hljs-keyword">if</span> (nums[i] &gt; topEle) &#123;<br>                minHeap.poll();<br>                minHeap.offer(nums[i]);<br>            &#125;<br>        &#125;<br>        <span class="hljs-keyword">return</span> minHeap.peek();<br>    &#125;<br></code></pre></td></tr></table></figure>

<h4 id="堆排序"><a href="#堆排序" class="headerlink" title="堆排序"></a>堆排序</h4><ul>
<li><p>堆排序是什么原理呢？</p>
</li>
<li><h5 id="构造大顶堆，依次取出堆顶元素；每取出一个堆顶元素后，重新调整堆，这样得到的序列就是从大到小排序的（降序排序）"><a href="#构造大顶堆，依次取出堆顶元素；每取出一个堆顶元素后，重新调整堆，这样得到的序列就是从大到小排序的（降序排序）" class="headerlink" title="构造大顶堆，依次取出堆顶元素；每取出一个堆顶元素后，重新调整堆，这样得到的序列就是从大到小排序的（降序排序）"></a>构造大顶堆，依次取出堆顶元素；每取出一个堆顶元素后，重新调整堆，这样得到的序列就是从大到小排序的（降序排序）</h5></li>
<li><h5 id="小顶堆同理，得到的序列就是从小到大排序的（升序排序）"><a href="#小顶堆同理，得到的序列就是从小到大排序的（升序排序）" class="headerlink" title="小顶堆同理，得到的序列就是从小到大排序的（升序排序）"></a>小顶堆同理，得到的序列就是从小到大排序的（升序排序）</h5></li>
<li><p><strong>升序用小</strong>，<strong>降序用大</strong> <strong>（2023/09/30晚）</strong></p>
</li>
</ul>
<h4 id="合并K个有序链表"><a href="#合并K个有序链表" class="headerlink" title="合并K个有序链表"></a>合并K个有序链表</h4><h2 id="图算法"><a href="#图算法" class="headerlink" title="图算法"></a>图算法</h2><h3 id="基础知识（青铜挑战）-10"><a href="#基础知识（青铜挑战）-10" class="headerlink" title="基础知识（青铜挑战）"></a>基础知识（青铜挑战）</h3><ul>
<li>有向图、无向图</li>
<li>边、弧</li>
<li>节点的度、入度、出度</li>
<li>描述节点之间的关系：&lt;V1,V2&gt;  (V1,V2) </li>
<li>路径、回路</li>
<li>权、网</li>
<li>连通图、连通分量、强连通图</li>
<li>生成树、生成森林</li>
</ul>
<h3 id="实战训练（白银挑战）-10"><a href="#实战训练（白银挑战）-10" class="headerlink" title="实战训练（白银挑战）"></a>实战训练（白银挑战）</h3><h4 id="图的存储"><a href="#图的存储" class="headerlink" title="图的存储"></a>图的存储</h4><h4 id="图的遍历"><a href="#图的遍历" class="headerlink" title="图的遍历"></a>图的遍历</h4><h4 id="最小生成树问题"><a href="#最小生成树问题" class="headerlink" title="最小生成树问题"></a>最小生成树问题</h4><h4 id="最短路径问题"><a href="#最短路径问题" class="headerlink" title="最短路径问题"></a>最短路径问题</h4><h2 id="排序算法"><a href="#排序算法" class="headerlink" title="排序算法"></a>排序算法</h2><ul>
<li><h5 id="推荐阅读：十大经典排序算法总结-JavaGuide-Java面试-学习指南"><a href="#推荐阅读：十大经典排序算法总结-JavaGuide-Java面试-学习指南" class="headerlink" title="推荐阅读：十大经典排序算法总结 | JavaGuide(Java面试 + 学习指南)"></a>推荐阅读：<a target="_blank" rel="noopener" href="https://javaguide.cn/cs-basics/algorithms/10-classical-sorting-algorithms.html#%E5%9B%BE%E8%A7%A3%E7%AE%97%E6%B3%95-3">十大经典排序算法总结 | JavaGuide(Java面试 + 学习指南)</a></h5></li>
<li><h5 id="各算法动态图演示：各算法动态图演示（2023-10-17完晚）"><a href="#各算法动态图演示：各算法动态图演示（2023-10-17完晚）" class="headerlink" title="各算法动态图演示：各算法动态图演示（2023/10/17完晚）"></a>各算法动态图演示：<a target="_blank" rel="noopener" href="https://www.yijiyong.com/algorithm/visual/01-commondy.html">各算法动态图演示</a>（2023/10/17完晚）</h5></li>
<li><h5 id="数据结构与算法精讲：数据结构与算法精讲"><a href="#数据结构与算法精讲：数据结构与算法精讲" class="headerlink" title="数据结构与算法精讲：数据结构与算法精讲"></a>数据结构与算法精讲：<a target="_blank" rel="noopener" href="https://visualgo.net/en">数据结构与算法精讲</a></h5></li>
</ul>
<h3 id="冒泡排序"><a href="#冒泡排序" class="headerlink" title="冒泡排序"></a>冒泡排序</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 冒泡排序</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@param</span> arr</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@return</span> arr</span><br><span class="hljs-comment"> */</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-type">int</span>[] bubbleSort(<span class="hljs-type">int</span>[] arr) &#123;<br>    <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">1</span>; i &lt; arr.length; i++) &#123;<br>        <span class="hljs-comment">// Set a flag, if true, that means the loop has not been swapped,</span><br>        <span class="hljs-comment">// that is, the sequence has been ordered, the sorting has been completed.</span><br>        <span class="hljs-type">boolean</span> <span class="hljs-variable">flag</span> <span class="hljs-operator">=</span> <span class="hljs-literal">true</span>;<br>        <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">j</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; j &lt; arr.length - i; j++) &#123;<br>            <span class="hljs-keyword">if</span> (arr[j] &gt; arr[j + <span class="hljs-number">1</span>]) &#123;<br>                <span class="hljs-type">int</span> <span class="hljs-variable">tmp</span> <span class="hljs-operator">=</span> arr[j];<br>                arr[j] = arr[j + <span class="hljs-number">1</span>];<br>                arr[j + <span class="hljs-number">1</span>] = tmp;<br>       <span class="hljs-comment">// Change flag</span><br>                flag = <span class="hljs-literal">false</span>;<br>            &#125;<br>        &#125;<br>        <span class="hljs-keyword">if</span> (flag) &#123;<br>            <span class="hljs-keyword">break</span>;<br>        &#125;<br>    &#125;<br>    <span class="hljs-keyword">return</span> arr;<br>&#125;<br></code></pre></td></tr></table></figure>

<h3 id="选择排序"><a href="#选择排序" class="headerlink" title="选择排序"></a>选择排序</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 选择排序</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@param</span> arr</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@return</span> arr</span><br><span class="hljs-comment"> */</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-type">int</span>[] selectionSort(<span class="hljs-type">int</span>[] arr) &#123;<br>    <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i &lt; arr.length - <span class="hljs-number">1</span>; i++) &#123;<br>        <span class="hljs-type">int</span> <span class="hljs-variable">minIndex</span> <span class="hljs-operator">=</span> i;<br>        <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">j</span> <span class="hljs-operator">=</span> i + <span class="hljs-number">1</span>; j &lt; arr.length; j++) &#123;<br>            <span class="hljs-keyword">if</span> (arr[j] &lt; arr[minIndex]) &#123;<br>                minIndex = j;<br>            &#125;<br>        &#125;<br>        <span class="hljs-keyword">if</span> (minIndex != i) &#123;<br>            <span class="hljs-type">int</span> <span class="hljs-variable">tmp</span> <span class="hljs-operator">=</span> arr[i];<br>            arr[i] = arr[minIndex];<br>            arr[minIndex] = tmp;<br>        &#125;<br>    &#125;<br>    <span class="hljs-keyword">return</span> arr;<br>&#125;<br><br></code></pre></td></tr></table></figure>

<h3 id="插入排序"><a href="#插入排序" class="headerlink" title="插入排序"></a>插入排序</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 插入排序</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@param</span> arr</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@return</span> arr</span><br><span class="hljs-comment"> */</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-type">int</span>[] insertionSort(<span class="hljs-type">int</span>[] arr) &#123;<br>    <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">1</span>; i &lt; arr.length; i++) &#123;<br>        <span class="hljs-type">int</span> <span class="hljs-variable">preIndex</span> <span class="hljs-operator">=</span> i - <span class="hljs-number">1</span>;<br>        <span class="hljs-type">int</span> <span class="hljs-variable">current</span> <span class="hljs-operator">=</span> arr[i];<br>        <span class="hljs-keyword">while</span> (preIndex &gt;= <span class="hljs-number">0</span> &amp;&amp; current &lt; arr[preIndex]) &#123;<br>            arr[preIndex + <span class="hljs-number">1</span>] = arr[preIndex];<br>            preIndex -= <span class="hljs-number">1</span>;<br>        &#125;<br>        arr[preIndex + <span class="hljs-number">1</span>] = current;<br>    &#125;<br>    <span class="hljs-keyword">return</span> arr;<br>&#125;<br><br></code></pre></td></tr></table></figure>

<h3 id="快速排序-1"><a href="#快速排序-1" class="headerlink" title="快速排序"></a>快速排序</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">quickSort</span><span class="hljs-params">(<span class="hljs-type">int</span>[] arr, <span class="hljs-type">int</span> low, <span class="hljs-type">int</span> high)</span> &#123;<br>        <span class="hljs-comment">// 判断递归结束的条件</span><br>        <span class="hljs-keyword">if</span> (low &gt;= high) &#123;<br>            <span class="hljs-keyword">return</span>;<br>        &#125;<br>        <span class="hljs-comment">// 定义快慢指针、基准值</span><br>        <span class="hljs-type">int</span> <span class="hljs-variable">point</span> <span class="hljs-operator">=</span> arr[low];<br><br>        <span class="hljs-type">int</span> <span class="hljs-variable">slow</span> <span class="hljs-operator">=</span> low;<br>        <span class="hljs-type">int</span> <span class="hljs-variable">fast</span> <span class="hljs-operator">=</span> low + <span class="hljs-number">1</span>;<br><br>        <span class="hljs-comment">// 快指针检索大数</span><br>        <span class="hljs-keyword">while</span> (fast &lt;= high) &#123;<br>            <span class="hljs-comment">// 慢指针维护小数</span><br>            <span class="hljs-keyword">if</span> (arr[fast] &lt; point) &#123;<br>                <span class="hljs-comment">// todo 0</span><br>                slow++;<br>                <span class="hljs-type">int</span> <span class="hljs-variable">temp</span> <span class="hljs-operator">=</span> arr[slow];<br>                arr[slow] = arr[fast];<br>                arr[fast] = temp;<br>            &#125;<br>            fast++;<br>        &#125;<br><br>        <span class="hljs-comment">// 返回基准值</span><br>        <span class="hljs-comment">// todo 1</span><br>        <span class="hljs-type">int</span> <span class="hljs-variable">temp</span> <span class="hljs-operator">=</span> arr[slow];<br>        arr[slow] = arr[low];<br>        arr[low] = temp;<br><br>        quickSort(arr, low, slow - <span class="hljs-number">1</span>);<br>        quickSort(arr, slow + <span class="hljs-number">1</span>, high);<br>    &#125;<br></code></pre></td></tr></table></figure>

<h3 id="各排序算法比较"><a href="#各排序算法比较" class="headerlink" title="各排序算法比较"></a>各排序算法比较</h3><p><img src="https://gitee.com/deng-2022/pictures/raw/master/images/20190917200246823.png" alt="各排序算法比较"></p>
<h2 id="贪心"><a href="#贪心" class="headerlink" title="贪心"></a>贪心</h2><ul>
<li>每一步中寻找最优解，或者说相对最优解的算法思想</li>
</ul>
<h3 id="小试牛刀"><a href="#小试牛刀" class="headerlink" title="小试牛刀"></a>小试牛刀</h3><h4 id="分发饼干"><a href="#分发饼干" class="headerlink" title="分发饼干"></a>分发饼干</h4><ul>
<li>你有几个孩子和几摞饼干，孩子有胃口，饼干有数量，尽可能分发饼干以满足胃口大的孩子</li>
<li>解题思路：<ul>
<li>要尽可能满足胃口大的孩子，我们不妨把孩子们按胃口排序，从胃口最大的孩子开始，分发饼干</li>
<li>当然，与之对应的，每一摞饼干也要按照数量排好序</li>
<li>从胃口大的孩子开始，从数量多的饼干开始，依次给孩子分配</li>
<li>如果饼干不满足该孩子的胃口（此时饼干数量最多了），那就分配给下一个孩子（他的胃口更小），看能不能满足这个孩子的胃口</li>
<li>以此类推，饼干不满足孩子胃口，就找下一个孩子；满足了，再拿下一摞饼干进行分配</li>
<li>这样就能保证：<strong>能够合理分配这些饼干来尽可能满足胃口大的孩子</strong> <strong>（2023/10/19晚）</strong></li>
</ul>
</li>
<li>具体代码如下：</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 分发饼干</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> g 孩子数</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> s 饼干数</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> 成功分配的孩子数</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-type">int</span> <span class="hljs-title function_">findContentChildren</span><span class="hljs-params">(<span class="hljs-type">int</span>[] g, <span class="hljs-type">int</span>[] s)</span> &#123;<br>        <span class="hljs-comment">// 排列孩子、饼干</span><br>        Arrays.sort(g);<br>        Arrays.sort(s);<br>        <span class="hljs-comment">// 记录成功分配的孩子数、每摞饼干数</span><br>        <span class="hljs-type">int</span> <span class="hljs-variable">count</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;<br>        <span class="hljs-type">int</span> <span class="hljs-variable">start</span> <span class="hljs-operator">=</span> s.length - <span class="hljs-number">1</span>;<br>        <span class="hljs-comment">// 遍历孩子的胃口</span><br>        <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">index</span> <span class="hljs-operator">=</span> g.length - <span class="hljs-number">1</span>; index &gt;= <span class="hljs-number">0</span>; index--) &#123;<br>            <span class="hljs-keyword">if</span> (start &gt;= <span class="hljs-number">0</span> &amp;&amp; g[index] &lt;= s[start]) &#123;<br>                start--;<br>                count++;<br>            &#125;<br>        &#125;<br>        <span class="hljs-comment">// 返回成功分配的孩子数</span><br>        <span class="hljs-keyword">return</span> count;<br>    &#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 分发饼干</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> g 孩子数</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> s 饼干数</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> 成功分配的孩子数</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-type">int</span> <span class="hljs-title function_">findContentChildren</span><span class="hljs-params">(<span class="hljs-type">int</span>[] g, <span class="hljs-type">int</span>[] s)</span> &#123;<br>        <span class="hljs-comment">// 排列孩子、饼干</span><br>        Arrays.sort(g);<br>        Arrays.sort(s);<br>        <span class="hljs-comment">// 记录成功分配的孩子数、每摞饼干数</span><br>        <span class="hljs-type">int</span> <span class="hljs-variable">count</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;<br>        <span class="hljs-type">int</span> <span class="hljs-variable">index</span> <span class="hljs-operator">=</span> g.length - <span class="hljs-number">1</span>;<br>        <span class="hljs-type">int</span> <span class="hljs-variable">start</span> <span class="hljs-operator">=</span> s.length - <span class="hljs-number">1</span>;<br>        <span class="hljs-comment">// 遍历孩子的胃口</span><br>        <span class="hljs-keyword">while</span> (index &gt;= <span class="hljs-number">0</span>) &#123;<br>            <span class="hljs-keyword">if</span> (start &gt;= <span class="hljs-number">0</span> &amp;&amp; g[index] &lt;= s[start]) &#123;<br>                start--; <br>                count++;<br>            &#125;<br>            index--;<br>        &#125;<br>        <span class="hljs-comment">// 返回成功分配的孩子数</span><br>        <span class="hljs-keyword">return</span> count;<br>    &#125;<br></code></pre></td></tr></table></figure>

<h4 id="柠檬水找零"><a href="#柠檬水找零" class="headerlink" title="柠檬水找零"></a>柠檬水找零</h4><ul>
<li><h5 id="情景："><a href="#情景：" class="headerlink" title="情景："></a>情景：</h5><ul>
<li>你在卖柠檬水，一杯柠檬水 5 块钱，顾客排队购买你的柠檬水</li>
<li>每位顾客只买一杯，他们给你的金额有5块、10块和20块</li>
<li>最初你手头的余额为0，确保给每位顾客正确找零</li>
</ul>
</li>
<li><h5 id="解题思路："><a href="#解题思路：" class="headerlink" title="解题思路："></a>解题思路：</h5><ul>
<li><p>顾客给你5块钱，你就照收不误</p>
</li>
<li><p>顾客给你10块钱，你得找他5块钱</p>
</li>
<li><p>顾客给你20块钱，你得找他5块钱和10块钱（优先），或者找他3张5块钱（其次）</p>
</li>
<li><p>也就是说，20块钱是不会用来找零钱的，我们就不关注手头20块钱的数目了</p>
</li>
<li><p>如果你找不了零钱，那一定是5块和10块其一不够找零钱了，我们只需关注这两种面值即可</p>
</li>
<li><h5 id="确保给每位顾客正确找零，就要保证每处理一个顾客（每次找完零钱）后，手头的5块和10块不能“欠费”（2023-10-19晚）"><a href="#确保给每位顾客正确找零，就要保证每处理一个顾客（每次找完零钱）后，手头的5块和10块不能“欠费”（2023-10-19晚）" class="headerlink" title="确保给每位顾客正确找零，就要保证每处理一个顾客（每次找完零钱）后，手头的5块和10块不能“欠费”（2023/10/19晚）"></a>确保给每位顾客正确找零，就要保证每处理一个顾客（每次找完零钱）后，手头的5块和10块不能“欠费”（2023/10/19晚）</h5></li>
</ul>
</li>
<li><p>具体代码如下：</p>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">    * 柠檬水找零</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@param</span> bills 排队中的顾客</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@return</span> 能否全部找零</span><br><span class="hljs-comment">    */</span><br>   <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-type">boolean</span> <span class="hljs-title function_">lemonadeChange</span><span class="hljs-params">(<span class="hljs-type">int</span>[] bills)</span> &#123;<br>       <span class="hljs-comment">// 这里只表示5元和10元纸币的数量，而不是总金额</span><br>       <span class="hljs-type">int</span> <span class="hljs-variable">cash_5</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;<br>       <span class="hljs-type">int</span> <span class="hljs-variable">cash_10</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;<br>       <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i &lt; bills.length; i++) &#123;<br>           <span class="hljs-comment">// 收到5块钱</span><br>           <span class="hljs-keyword">if</span> (bills[i] == <span class="hljs-number">5</span>) &#123;<br>               cash_5++;<br>               <span class="hljs-comment">// 收到10块钱</span><br>           &#125; <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (bills[i] == <span class="hljs-number">10</span>) &#123;<br>               cash_5--;<br>               cash_10++;<br>               <span class="hljs-comment">// 收到20块钱</span><br>           &#125; <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (bills[i] == <span class="hljs-number">20</span>) &#123;<br>               <span class="hljs-keyword">if</span> (cash_10 &gt; <span class="hljs-number">0</span>) &#123;<br>                   cash_10--;<br>                   cash_5--;<br>               &#125; <span class="hljs-keyword">else</span> &#123;<br>                   cash_5 -= <span class="hljs-number">3</span>;<br>               &#125;<br>           &#125;<br>           <span class="hljs-keyword">if</span> (cash_5 &lt; <span class="hljs-number">0</span> || cash_10 &lt; <span class="hljs-number">0</span>) <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;<br>       &#125;<br>       <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;<br>   &#125;<br></code></pre></td></tr></table></figure>

<h4 id="分发糖果"><a href="#分发糖果" class="headerlink" title="分发糖果"></a>分发糖果</h4><ul>
<li><h5 id="情景：-1"><a href="#情景：-1" class="headerlink" title="情景："></a>情景：</h5><ul>
<li>你有几个孩子，糖果是无限制的，每个孩子都有各自的评分</li>
<li>要求<strong>根据他们的评分合理地给他们分发糖果</strong>，并要求用最少的糖果数完成</li>
</ul>
</li>
<li><p>解题思路：</p>
</li>
<li><p>具体代码如下：</p>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-type">int</span> <span class="hljs-title function_">candy</span><span class="hljs-params">(<span class="hljs-type">int</span>[] ratings)</span> &#123;<br>        <span class="hljs-type">int</span>[] candyVec = <span class="hljs-keyword">new</span> <span class="hljs-title class_">int</span>[ratings.length];<br>        candyVec[<span class="hljs-number">0</span>] = <span class="hljs-number">1</span>;<br>        <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">1</span>; i &lt; ratings.length; i++) &#123;<br>            <span class="hljs-keyword">if</span> (ratings[i] &gt; ratings[i - <span class="hljs-number">1</span>]) &#123;<br>                candyVec[i] = candyVec[i - <span class="hljs-number">1</span>] + <span class="hljs-number">1</span>;<br>            &#125; <span class="hljs-keyword">else</span> &#123;<br>                candyVec[i] = <span class="hljs-number">1</span>;<br>            &#125;<br>        &#125;<br>        <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> ratings.length - <span class="hljs-number">2</span>; i &gt;= <span class="hljs-number">0</span>; i--) &#123;<br>            <span class="hljs-keyword">if</span> (ratings[i] &gt; ratings[i + <span class="hljs-number">1</span>]) &#123;<br>                candyVec[i] = Math.max(candyVec[i], candyVec[i + <span class="hljs-number">1</span>] + <span class="hljs-number">1</span>);<br>            &#125;<br>        &#125;<br>        <span class="hljs-type">int</span> <span class="hljs-variable">ans</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;<br>        <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> s : candyVec) &#123;<br>            ans += s;<br>        &#125;<br>        <span class="hljs-keyword">return</span> ans;<br>    &#125;<br></code></pre></td></tr></table></figure>

<h3 id="高频问题"><a href="#高频问题" class="headerlink" title="高频问题"></a>高频问题</h3><h4 id="判断区间是否重叠"><a href="#判断区间是否重叠" class="headerlink" title="判断区间是否重叠"></a>判断区间是否重叠</h4><ul>
<li>情景：</li>
</ul>
<blockquote>
<p>输入：intervals = [[0, 30], [15, 20], [5, 10]]</p>
<p>解释：存在重叠区间</p>
<p>详情如下图所示：</p>
</blockquote>
<p><img src="https://gitee.com/deng-2022/pictures/raw/master/images/1692434389326-17ec91dd-317a-40b3-9423-1a634d8a5f77.png" alt="image.png"></p>
<ul>
<li><h5 id="解题思路：（2023-10-22晚）"><a href="#解题思路：（2023-10-22晚）" class="headerlink" title="解题思路：（2023/10/22晚）"></a>解题思路：（2023/10/22晚）</h5><ul>
<li>遍历每一个区间，相邻的两个区间作比较</li>
<li>如果前区间的尾部大于后区间的首部，就说明两区间重叠了</li>
</ul>
</li>
<li><h5 id="具体代码如下：-17"><a href="#具体代码如下：-17" class="headerlink" title="具体代码如下："></a>具体代码如下：</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-type">boolean</span> <span class="hljs-title function_">canAttendMeetings</span><span class="hljs-params">(<span class="hljs-type">int</span>[][] intervals)</span> &#123;<br>      <span class="hljs-comment">// 将区间按照会议开始实现升序排序</span><br>      Arrays.sort(intervals, (v1, v2) -&gt; v1[<span class="hljs-number">0</span>] - v2[<span class="hljs-number">0</span>]);<br>      <span class="hljs-comment">// 遍历会议，如果下一个会议在前一个会议结束之前就开始了，返回 false。</span><br>      <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">1</span>; i &lt; intervals.length; i++) &#123;<br>          <span class="hljs-keyword">if</span> (intervals[i][<span class="hljs-number">0</span>] &lt; intervals[i - <span class="hljs-number">1</span>][<span class="hljs-number">1</span>]) &#123;<br>              <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;<br>          &#125;<br>      &#125;<br>      <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;<br>  &#125;<br></code></pre></td></tr></table></figure>

<h4 id="区间合并"><a href="#区间合并" class="headerlink" title="区间合并"></a>区间合并</h4><ul>
<li><h5 id="情景：-2"><a href="#情景：-2" class="headerlink" title="情景："></a>情景：</h5></li>
</ul>
<blockquote>
<p>输入：intervals = [[1, 3], [2, 6], [8, 10], [15, 18]]</p>
<p>输出：[[1, 6], [8, 10], [15, 18]]</p>
<p>解释：区间 [1, 3] 和 [2, 6] 重叠, 将它们合并为 [1, 6]</p>
<p>详情如下图所示：</p>
</blockquote>
<p><img src="https://gitee.com/deng-2022/pictures/raw/master/images/1692434413988-42933d26-7e2e-4d5e-8e5f-fd7c06860b03.png" alt="image.png"></p>
<ul>
<li><h5 id="解题思路（2023-10-22晚）"><a href="#解题思路（2023-10-22晚）" class="headerlink" title="解题思路（2023/10/22晚）"></a>解题思路（2023/10/22晚）</h5><ul>
<li>还是遍历每一个区间，判断相邻的两个区间是否重合</li>
<li>如果不重合，就继续向后遍历区间；如果重合，就合并两区间·</li>
<li>判断是否重合就能确定是否需要合并了，那么具体该如何合并呢？我们拿一个新的数组来维护合并区间后的新数组<ul>
<li>合并：合并后的区间尾部取重合区间尾部的最大的那个，继续向后遍历区间</li>
<li>维护新的区间，一直比较新数组的尾部，直到不合并才开辟新区间（++idx）</li>
</ul>
</li>
</ul>
</li>
<li><h5 id="具体代码如下：-18"><a href="#具体代码如下：-18" class="headerlink" title="具体代码如下："></a>具体代码如下：</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-type">int</span>[][] merge(<span class="hljs-type">int</span>[][] intervals) &#123;<br>      <span class="hljs-comment">// 先按照区间起始位置排序</span><br>      Arrays.sort(intervals, (v1, v2) -&gt; v1[<span class="hljs-number">0</span>] - v2[<span class="hljs-number">0</span>]);<br>      <span class="hljs-comment">// 遍历区间</span><br>      <span class="hljs-type">int</span>[][] res = <span class="hljs-keyword">new</span> <span class="hljs-title class_">int</span>[intervals.length][<span class="hljs-number">2</span>];<br>      <span class="hljs-type">int</span> <span class="hljs-variable">idx</span> <span class="hljs-operator">=</span> -<span class="hljs-number">1</span>;<br>      <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span>[] interval : intervals) &#123;<br>          <span class="hljs-comment">// 如果结果数组是空的，或者当前区间的起始位置 &gt; 结果数组中最后区间的终止位置，说明不重叠。</span><br>          <span class="hljs-comment">// 则不合并，直接将当前区间加入结果数组。</span><br>          <span class="hljs-keyword">if</span> (idx == -<span class="hljs-number">1</span> || interval[<span class="hljs-number">0</span>] &gt; res[idx][<span class="hljs-number">1</span>]) &#123;<br>              res[++idx] = interval;<br>          &#125; <span class="hljs-keyword">else</span> &#123;<br>              <span class="hljs-comment">// 反之说明重叠，则将当前区间合并至结果数组的最后区间</span><br>              res[idx][<span class="hljs-number">1</span>] = Math.max(res[idx][<span class="hljs-number">1</span>], interval[<span class="hljs-number">1</span>]);<br>          &#125;<br>      &#125;<br>      <span class="hljs-keyword">return</span> Arrays.copyOf(res, idx + <span class="hljs-number">1</span>);<br>  &#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">// 先按照区间起始位置排序</span><br>     Arrays.sort(intervals, (v1, v2) -&gt; v1[<span class="hljs-number">0</span>] - v2[<span class="hljs-number">0</span>]);<br>     <span class="hljs-comment">// 遍历区间</span><br>     <span class="hljs-type">int</span>[][] res = <span class="hljs-keyword">new</span> <span class="hljs-title class_">int</span>[intervals.length][<span class="hljs-number">2</span>];<br><br>     <span class="hljs-type">int</span> <span class="hljs-variable">idx</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;<br>     <span class="hljs-keyword">if</span> (intervals.length &gt; <span class="hljs-number">0</span>) &#123;<br>         res[idx] = intervals[<span class="hljs-number">0</span>];<br>     &#125;<br><br>     <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">1</span>; i &lt; intervals.length; i++) &#123;<br>         <span class="hljs-comment">// 如果结果数组是空的，或者当前区间的起始位置 &gt; 结果数组中最后区间的终止位置，说明不重叠。</span><br>         <span class="hljs-comment">// 则不合并，直接将当前区间加入结果数组。</span><br>         <span class="hljs-keyword">if</span> (intervals[i][<span class="hljs-number">0</span>] &gt; res[idx][<span class="hljs-number">1</span>]) &#123;<br>             res[++idx] = intervals[i];<br>         &#125; <span class="hljs-keyword">else</span> &#123;<br>             <span class="hljs-comment">// 反之说明重叠，则将当前区间合并至结果数组的最后区间</span><br>             res[idx][<span class="hljs-number">1</span>] = Math.max(res[idx][<span class="hljs-number">1</span>], intervals[i][<span class="hljs-number">1</span>]);<br>         &#125;<br>     &#125;<br>     <span class="hljs-keyword">return</span> Arrays.copyOf(res, idx + <span class="hljs-number">1</span>);<br></code></pre></td></tr></table></figure>

<h4 id="区间插入"><a href="#区间插入" class="headerlink" title="区间插入"></a>区间插入</h4><ul>
<li><h5 id="情景"><a href="#情景" class="headerlink" title="情景"></a>情景</h5></li>
</ul>
<blockquote>
<p>输入：intervals = [[1, 3], [6, 9]], newInterval = [2, 5]</p>
<p>输出：[1, 5], [6, 9]</p>
<p>解释：新区间 [2, 5] 和 [1, 3] 重叠, 因此合并成为 [1, 5]</p>
<p>详情可参考下图：</p>
</blockquote>
<p><img src="https://gitee.com/deng-2022/pictures/raw/master/images/1692434440023-0e7d7af8-dbfc-4f06-a635-44234d465722.png" alt="image.png"></p>
<ul>
<li><h5 id="解题思路（2023-10-22晚）-1"><a href="#解题思路（2023-10-22晚）-1" class="headerlink" title="解题思路（2023/10/22晚）"></a>解题思路（2023/10/22晚）</h5><ul>
<li>我们仍然拿一个新数组来维护结果集</li>
<li>遍历所有区间，将新区间左边且不重合的区间加入结果集（区间尾部小于新区间首部，就说明该区间在新区间的左边且不重合）</li>
<li>再次遍历所有区间，合并与新区间重合的区间（在经过第一轮遍历筛选后，这次遍历时，区间首部小于新区间尾部的都属于重合）</li>
<li>最后剩下的区间就属于新区间右边且不重合的区间了，直接挨个放进结果集就行</li>
</ul>
</li>
<li><h5 id="具体代码如下：-19"><a href="#具体代码如下：-19" class="headerlink" title="具体代码如下："></a>具体代码如下：</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-type">int</span>[][] insert(<span class="hljs-type">int</span>[][] intervals, <span class="hljs-type">int</span>[] newInterval) &#123;<br>        <span class="hljs-type">int</span>[][] res = <span class="hljs-keyword">new</span> <span class="hljs-title class_">int</span>[intervals.length + <span class="hljs-number">1</span>][<span class="hljs-number">2</span>];<br>        <span class="hljs-type">int</span> <span class="hljs-variable">idx</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;<br>    <br>        <span class="hljs-comment">// 遍历区间列表：</span><br>        <span class="hljs-comment">// 首先将新区间左边且相离的区间加入结果集</span><br>        <span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;<br>        <span class="hljs-keyword">while</span> (i &lt; intervals.length &amp;&amp; intervals[i][<span class="hljs-number">1</span>] &lt; newInterval[<span class="hljs-number">0</span>]) &#123;<br>            res[idx++] = intervals[i++];<br>        &#125;<br>    <br>        <span class="hljs-comment">// 判断当前区间是否与新区间重叠，重叠的话就进行合并，直到遍历到当前区间在新区间的右边且相离，</span><br>        <span class="hljs-comment">// 将最终合并后的新区间加入结果集</span><br>        <span class="hljs-keyword">while</span> (i &lt; intervals.length &amp;&amp; intervals[i][<span class="hljs-number">0</span>] &lt;= newInterval[<span class="hljs-number">1</span>]) &#123;<br>            newInterval[<span class="hljs-number">0</span>] = Math.min(intervals[i][<span class="hljs-number">0</span>], newInterval[<span class="hljs-number">0</span>]);<br>            newInterval[<span class="hljs-number">1</span>] = Math.max(intervals[i][<span class="hljs-number">1</span>], newInterval[<span class="hljs-number">1</span>]);<br>            i++;<br>        &#125;<br>    <br>        res[idx++] = newInterval;<br>        <span class="hljs-comment">// 最后将新区间右边且相离的区间加入结果集</span><br>        <span class="hljs-keyword">while</span> (i &lt; intervals.length) &#123;<br>            res[idx++] = intervals[i++];<br>        &#125;<br><br>        <span class="hljs-keyword">return</span> Arrays.copyOf(res, idx);<br>    &#125;<br></code></pre></td></tr></table></figure>

<h2 id="滑动窗口"><a href="#滑动窗口" class="headerlink" title="滑动窗口"></a>滑动窗口</h2><h3 id="高频问题-1"><a href="#高频问题-1" class="headerlink" title="高频问题"></a>高频问题</h3><h4 id="最小覆盖子串"><a href="#最小覆盖子串" class="headerlink" title="最小覆盖子串"></a>最小覆盖子串</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">// 用于记录需要的字符和窗口中的字符及其出现的次数</span><br>   Map&lt;Character, Integer&gt; need = <span class="hljs-keyword">new</span> <span class="hljs-title class_">HashMap</span>&lt;&gt;();<br>   Map&lt;Character, Integer&gt; window = <span class="hljs-keyword">new</span> <span class="hljs-title class_">HashMap</span>&lt;&gt;();<br>   <span class="hljs-comment">// 统计 t 中各字符出现次数</span><br>   <span class="hljs-keyword">for</span> (<span class="hljs-type">char</span> c : t.toCharArray()) <br>       need.put(c, need.getOrDefault(c, <span class="hljs-number">0</span>) + <span class="hljs-number">1</span>);<br><br>   <span class="hljs-type">int</span> <span class="hljs-variable">left</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>, right = <span class="hljs-number">0</span>;<br>   <span class="hljs-type">int</span> <span class="hljs-variable">valid</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; <span class="hljs-comment">// 窗口中满足需要的字符个数</span><br>   <span class="hljs-comment">// 记录最小覆盖子串的起始索引及长度</span><br>   <span class="hljs-type">int</span> <span class="hljs-variable">start</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>, len = Integer.MAX_VALUE;<br>   <span class="hljs-keyword">while</span> (right &lt; s.length()) &#123;<br>       <span class="hljs-comment">// c 是将移入窗口的字符</span><br>       <span class="hljs-type">char</span> <span class="hljs-variable">c</span> <span class="hljs-operator">=</span> s.charAt(right);<br>       <span class="hljs-comment">// 扩大窗口</span><br>       right++;<br>       <span class="hljs-comment">// 进行窗口内数据的一系列更新</span><br>       <span class="hljs-keyword">if</span> (need.containsKey(c)) &#123;<br>           window.put(c, window.getOrDefault(c, <span class="hljs-number">0</span>) + <span class="hljs-number">1</span>);<br>           <span class="hljs-keyword">if</span> (window.get(c).equals(need.get(c)))<br>               valid++; <span class="hljs-comment">// 只有当 window[c] 和 need[c] 对应的出现次数一致时，才能满足条件，valid 才能 +1</span><br>       &#125;<br><br>       <span class="hljs-comment">// 判断左侧窗口是否要收缩</span><br>       <span class="hljs-keyword">while</span> (valid == need.size()) &#123;<br>           <span class="hljs-comment">// 更新最小覆盖子串</span><br>           <span class="hljs-keyword">if</span> (right - left &lt; len) &#123;<br>               start = left;<br>               len = right - left;<br>           &#125;<br>           <span class="hljs-comment">// d 是将移出窗口的字符</span><br>           <span class="hljs-type">char</span> <span class="hljs-variable">d</span> <span class="hljs-operator">=</span> s.charAt(left);<br>           <span class="hljs-comment">// 缩小窗口</span><br>           left++;<br>           <span class="hljs-comment">// 进行窗口内数据的一系列更新</span><br>           <span class="hljs-keyword">if</span> (need.containsKey(d)) &#123;<br>               <span class="hljs-keyword">if</span> (window.get(d).equals(need.get(d)))<br>                   valid--; <span class="hljs-comment">// 只有当 window[d] 内的出现次数和 need[d] 相等时，才能 -1</span><br>               window.put(d, window.get(d) - <span class="hljs-number">1</span>);<br>           &#125;<br>       &#125;<br>   &#125;<br><br>   <span class="hljs-comment">// 返回最小覆盖子串</span><br>   <span class="hljs-keyword">return</span> len == Integer.MAX_VALUE ?<br>       <span class="hljs-string">&quot;&quot;</span> : s.substring(start, start + len);<br></code></pre></td></tr></table></figure>

<h4 id="字符串排列"><a href="#字符串排列" class="headerlink" title="字符串排列"></a>字符串排列</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><code class="hljs java">HashMap&lt;Character, Integer&gt; need = <span class="hljs-keyword">new</span> <span class="hljs-title class_">HashMap</span>&lt;&gt;();<br>   HashMap&lt;Character, Integer&gt; window = <span class="hljs-keyword">new</span> <span class="hljs-title class_">HashMap</span>&lt;&gt;();<br>   <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i &lt; t.length(); i++) &#123;<br>       <span class="hljs-type">char</span> <span class="hljs-variable">c</span> <span class="hljs-operator">=</span> t.charAt(i);<br>       need.put(c, need.getOrDefault(c, <span class="hljs-number">0</span>) + <span class="hljs-number">1</span>);<br>   &#125;<br><br>   <span class="hljs-type">int</span> <span class="hljs-variable">left</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>, right = <span class="hljs-number">0</span>;<br>   <span class="hljs-type">int</span> <span class="hljs-variable">valid</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;<br>   <span class="hljs-keyword">while</span> (right &lt; s.length()) &#123;<br>       <span class="hljs-type">char</span> <span class="hljs-variable">c</span> <span class="hljs-operator">=</span> s.charAt(right);<br>       right++;<br>       <span class="hljs-keyword">if</span> (need.containsKey(c)) &#123;<br>           window.put(c, window.getOrDefault(c, <span class="hljs-number">0</span>) + <span class="hljs-number">1</span>);<br>           <span class="hljs-keyword">if</span> (window.get(c).equals(need.get(c)))<br>               valid++;<br>       &#125;<br><br>       <span class="hljs-comment">// 判断左侧窗口是否要收缩</span><br>       <span class="hljs-keyword">while</span> (right - left &gt;= t.length()) &#123;<br>           <span class="hljs-comment">// 在这里判断是否找到了合法的子串</span><br>           <span class="hljs-keyword">if</span> (valid == need.size())<br>               <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;<br>           <span class="hljs-type">char</span> <span class="hljs-variable">d</span> <span class="hljs-operator">=</span> s.charAt(left);<br>           left++;<br>           <span class="hljs-keyword">if</span> (need.containsKey(d)) &#123;<br>               <span class="hljs-keyword">if</span> (window.get(d).equals(need.get(d)))<br>                   valid--;<br>               window.put(d, window.getOrDefault(d, <span class="hljs-number">0</span>) - <span class="hljs-number">1</span>);<br>           &#125;<br>       &#125;<br>   &#125;<br>   <span class="hljs-comment">// 未找到符合条件的子串</span><br>   <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;<br></code></pre></td></tr></table></figure>

<h4 id="找所有字母异位词"><a href="#找所有字母异位词" class="headerlink" title="找所有字母异位词"></a>找所有字母异位词</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><code class="hljs java">Map&lt;Character, Integer&gt; need = <span class="hljs-keyword">new</span> <span class="hljs-title class_">HashMap</span>&lt;&gt;();<br>  Map&lt;Character, Integer&gt; window = <span class="hljs-keyword">new</span> <span class="hljs-title class_">HashMap</span>&lt;&gt;();<br>  <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i &lt; t.length(); i++) &#123;<br>      <span class="hljs-type">char</span> <span class="hljs-variable">c</span> <span class="hljs-operator">=</span> t.charAt(i);<br>      need.put(c, need.getOrDefault(c, <span class="hljs-number">0</span>) + <span class="hljs-number">1</span>);<br>  &#125;<br><br>  <span class="hljs-type">int</span> <span class="hljs-variable">left</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>, right = <span class="hljs-number">0</span>;<br>  <span class="hljs-type">int</span> <span class="hljs-variable">valid</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;<br>  List&lt;Integer&gt; res = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayList</span>&lt;&gt;(); <span class="hljs-comment">// 记录结果</span><br>  <span class="hljs-keyword">while</span> (right &lt; s.length()) &#123;<br>      <span class="hljs-type">char</span> <span class="hljs-variable">c</span> <span class="hljs-operator">=</span> s.charAt(right);<br>      right++;<br>      <span class="hljs-comment">// 进行窗口内数据的一系列更新</span><br>      <span class="hljs-keyword">if</span> (need.containsKey(c)) &#123;<br>          window.put(c, window.getOrDefault(c, <span class="hljs-number">0</span>) + <span class="hljs-number">1</span>);<br>          <span class="hljs-keyword">if</span> (window.get(c).equals(need.get(c))) &#123;<br>              valid++;<br>          &#125;<br>      &#125;<br>      <span class="hljs-comment">// 判断左侧窗口是否要收缩</span><br>      <span class="hljs-keyword">while</span> (right - left &gt;= t.length()) &#123;<br>          <span class="hljs-comment">// 当窗口符合条件时，把起始索引加入 res</span><br>          <span class="hljs-keyword">if</span> (valid == need.size()) &#123;<br>              res.add(left);<br>          &#125;<br>          <span class="hljs-type">char</span> <span class="hljs-variable">d</span> <span class="hljs-operator">=</span> s.charAt(left);<br>          left++;<br>          <span class="hljs-comment">// 进行窗口内数据的一系列更新</span><br>          <span class="hljs-keyword">if</span> (need.containsKey(d)) &#123;<br>              <span class="hljs-keyword">if</span> (window.get(d).equals(need.get(d))) &#123;<br>                  valid--;<br>              &#125;<br>              window.put(d, window.get(d) - <span class="hljs-number">1</span>);<br>          &#125;<br>      &#125;<br>  &#125;<br>  <span class="hljs-keyword">return</span> res;<br></code></pre></td></tr></table></figure>

<h4 id="最长无重复字符串"><a href="#最长无重复字符串" class="headerlink" title="最长无重复字符串"></a>最长无重复字符串</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><code class="hljs java">Map&lt;Character, Integer&gt; window = <span class="hljs-keyword">new</span> <span class="hljs-title class_">HashMap</span>&lt;&gt;();<br><br>  <span class="hljs-type">int</span> <span class="hljs-variable">left</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>, right = <span class="hljs-number">0</span>;<br>  <span class="hljs-type">int</span> <span class="hljs-variable">res</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; <span class="hljs-comment">// 记录结果</span><br>  <span class="hljs-keyword">while</span> (right &lt; s.length()) &#123;<br>      <span class="hljs-type">char</span> <span class="hljs-variable">c</span> <span class="hljs-operator">=</span> s.charAt(right);<br>      right++;<br>      <span class="hljs-comment">// 进行窗口内数据的一系列更新</span><br>      window.put(c, window.getOrDefault(c, <span class="hljs-number">0</span>) + <span class="hljs-number">1</span>);<br>      <span class="hljs-comment">// 判断左侧窗口是否要收缩</span><br>      <span class="hljs-keyword">while</span> (window.get(c) &gt; <span class="hljs-number">1</span>) &#123;<br>          <span class="hljs-type">char</span> <span class="hljs-variable">d</span> <span class="hljs-operator">=</span> s.charAt(left);<br>          left++;<br>          <span class="hljs-comment">// 进行窗口内数据的一系列更新</span><br>          window.put(d, window.get(d) - <span class="hljs-number">1</span>);<br>      &#125;<br>      <span class="hljs-comment">// 在这里更新答案</span><br>      res = Math.max(res, right - left);<br>  &#125;<br>  <span class="hljs-keyword">return</span> res;<br></code></pre></td></tr></table></figure>


                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/memory_blog/categories/%E7%AE%97%E6%B3%95/" class="category-chain-item">算法</a>
  
  
    <span>></span>
    
  <a href="/memory_blog/categories/%E7%AE%97%E6%B3%95/%E7%BB%8F%E9%AA%8C/" class="category-chain-item">经验</a>
  
  

  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/memory_blog/tags/%E7%BB%8F%E9%AA%8C/">#经验</a>
      
        <a href="/memory_blog/tags/%E7%AE%97%E6%B3%95/">#算法</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>探索算法学习之旅：经验分享与解题思路</div>
      <div>https://test.atomgit.net/memory_blog/2023/07/17/探索算法学习之旅：经验分享与解题思路/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>Memory</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2023年7月17日</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>更新于</div>
          <div>2024年4月24日</div>
        </div>
      
      
        <div class="license-meta-item">
          <div>许可协议</div>
          <div>
            
              
              
                <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
                  <span class="hint--top hint--rounded" aria-label="BY - 署名">
                    <i class="iconfont icon-by"></i>
                  </span>
                </a>
              
            
          </div>
        </div>
      
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/memory_blog/2023/07/21/%E6%B5%B4%E7%81%AB%E9%87%8D%E7%94%9F%EF%BC%9A%E4%BB%8E%E6%8C%AB%E8%B4%A5%E5%88%B0%E6%88%90%E5%8A%9F%EF%BC%8C%E5%89%8D%E7%AB%AF%E6%A1%86%E6%9E%B6%E7%9A%84%E7%96%91%E9%9A%BE%E6%9D%82%E7%97%87%E8%A7%A3%E5%86%B3%E4%B9%8B%E9%81%93/" title="浴火重生：从挫败到成功，前端框架的疑难杂症解决之道">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">浴火重生：从挫败到成功，前端框架的疑难杂症解决之道</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/memory_blog/2023/07/15/%E8%A7%A3%E5%86%B3%20Git%20%E8%BF%9C%E7%A8%8B%E6%8E%A8%E9%80%81%E6%8A%A5%E9%94%99%E7%9A%84%E7%BB%88%E6%9E%81%E6%8C%87%E5%8D%97/" title="解决 Git 远程推送报错的终极指南">
                        <span class="hidden-mobile">解决 Git 远程推送报错的终极指南</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
  
  
    <article id="comments">
      

    </article>
  


          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header">
    <i class="iconfont icon-list"></i>
    <span>目录</span>
  </p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.fluid-dev.com/docs/guide/" target="_blank" rel="nofollow noopener"> <span>Memory | 个人文档站点</span> </a>
<i class="iconfont icon-love"></i> <a href="https://hexo.fluid-dev.com/docs/guide/" target="_blank" rel="nofollow noopener"> <span>配置指南 | Hexo Fluid 用户手册</span> </a>   
    </div>
  
  
    <div class="statistics">
  
  

  
    
      <span id="leancloud-site-pv-container" style="display: none">
        总访问量 
        <span id="leancloud-site-pv"></span>
         次
      </span>
    
    
      <span id="leancloud-site-uv-container" style="display: none">
        总访客数 
        <span id="leancloud-site-uv"></span>
         人
      </span>
    
    

  
</div>

  
  
    <!-- 备案信息 ICP for China -->
    <div class="beian">
  <span>
    <a href="http://beian.miit.gov.cn/" target="_blank" rel="nofollow noopener">
      京ICP证123456号
    </a>
  </span>
  
    
      <span>
        <a
          href="http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=12345678"
          rel="nofollow noopener"
          class="beian-police"
          target="_blank"
        >
          
            <span style="visibility: hidden; width: 0">|</span>
            <img src="/memory_blog/img/police_beian.png" alt="police-icon"/>
          
          <span>京公网安备12345678号</span>
        </a>
      </span>
    
  
</div>

  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/memory_blog/js/events.js" ></script>
<script  src="/memory_blog/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>







  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init(Object.assign({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      scrollSmooth    : true,
      includeTitleTags: true,
      headingsOffset  : -boardTop,
    }, CONFIG.toc));
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }

    Fluid.events.registerRefreshCallback(function() {
      if ('tocbot' in window) {
        tocbot.refresh();
        var toc = jQuery('#toc');
        if (toc.length === 0 || !tocbot) {
          return;
        }
        if (toc.find('.toc-list-item').length > 0) {
          toc.css('visibility', 'visible');
        }
      }
    });
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.11/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));

    Fluid.events.registerRefreshCallback(function() {
      if ('anchors' in window) {
        anchors.removeAll();
        var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
        var res = [];
        for (var item of el) {
          res.push('.markdown-body > ' + item.trim());
        }
        if (CONFIG.anchorjs.placement === 'left') {
          anchors.options.class = 'anchorjs-link-left';
        }
        anchors.add(res.join(', '));
      }
    });
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/memory_blog/js/local-search.js" ></script>

  <script defer src="/memory_blog/js/leancloud.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/memory_blog/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
